Assuming I have a bunch of sqlcmd commands in .cmd files, order alphabetically e.g.:
01.setup.cmd
02.version1.cmd
03.version2.cmd
04.version3.cmd
how could one sequentially execute these in correct order with another .cmd file?
On windows:
for /F "tokens=*" %a in ('dir /b *.cmd') do call "%a"
This just loops over the result of dir /b *.cmd calling each in turn.
explanation from the docs:
FOR /F processing of a text file consists of reading the file, one
line of text at a time and then breaking the line up into individual
items of data called 'tokens'. The DO command is then executed with
the parameter(s) set to the token(s) found.
So my command says:
"tokens=*" don't give me individual tokens, give me the whole line as one hit
%a - name the line variable %a (note: it'll needs to be escaped as %%a if you're putting it in a batch file
('dir /b *.cmd') This is the input that it'll loop over. A bare directory listing for all .cmd files
then what I want it to do. Call the command %a.
If I didn't add the tokens bit it would work fine until you find a space in the file names.
Related
I'm fairly new to batch, my problem is the following:
I have a long list of folders and need to delete the first 3 characters from each of their names. Think 01_Folder1, 02_Folder2, 03_Folder3 and so on. I've tried patching together pieces of CMD commands I've found on the web but could not come up with a script that does what I want it to do. I've even tried using VBScript as I'm more familiar with VB in general but failed to find a solution as well.
Is there an easy way to solve this?
Edit:
Here's my attempt; it's giving me a syntax error but as I am not versed enough in CMD, I cannot really see why:
setlocal enableextensions enabledelayedexpansion
for /d %%i in ("%~dp0*") do (set name=%%i && ren "!name!" "!name:~3!")
endlocal
The FOR command line does not work because of assigned to loop variable i is the name of a directory with full path and so removing the first three characters results in removing drive letter, colon and backslash from path of the directory and not the first three characters from directory name. Further the full qualified directory name is assigned with an additional space to environment variable name because of the space between %%i and operator &&.
One solution would be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /d %%i in ("%~dp0*") do set "name=%%~nxi" && ren "%%i" "!name:~3!"
endlocal
The disadvantage of this solution is that directory names with one or more exclamation marks in name or path are not processed correct because of enabled delayed expansion resulting in interpreting ! in full directory name as begin/end of a delayed expanded environment variable reference.
Another solution is quite simple with using just the command line:
#for /D %%i in ("%~dp0*_*") do for /F "tokens=1* delims=_" %%j in ("%%~nxi") do #ren "%%i" "%%k"
The outer FOR searches in directory of the batch file for non-hidden subdirectories matching the pattern *_*.
For each directory name assigned with full path to loop variable i one more FOR command is used which processes just the string after last backlash (directory name without path) and splits the string up into substrings (tokens).
The string delimiter is an underscore as defined with option delims=_. The option tokens=1* tells FOR to assign first underscore delimited string to specified loop variable j and everything after one or more underscores after first underscore delimited string to next but one loop variable k according to ASCII table.
The inner FOR would ignore a directory name on which first substring starts with a semicolon as being the default end of line character. But in this case no directory has ; at beginning of its name.
There is one problem remaining with this command line. It does not work on drives with FAT32 or exFAT as file system, just by chance on drives with NTFS file system. The reason is that the list of non-hidden directories changes in file system while the outer FOR iterates over the directory entries matching the pattern.
A better solution loads first the list of directories to rename into memory of Windows command process which is processing the batch file before starting with renaming the directories.
#for /F "delims=" %%i in ('dir "%~dp0*_*" /AD-H /B 2^>nul') do for /F "tokens=1* delims=_" %%j in ("%%i") do #ren "%~dp0%%i" "%%k"
FOR executes in this case in background one more command process with %ComSpec% /c and the command line within ' appended as additional arguments. So executed in background is with Windows installed to C:\Windows:
C:\Windows\System32\cmd.exe /c dir "C:\Batch\File\Path\*_*" /AD-H /B 2>nul
DIR searches in directory of the batch file for
non-hidden directories because of option /AD-H (attribute directory and not hidden)
matching the wildcard pattern *_*
and outputs just the directory names in bare format because of option /B without path to handle STDOUT (standard output) of background command process.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR respectively the Windows command process processing the batch file captures everything written to standard output handle of background command process and starts processing it as described above after started cmd.exe terminated itself. So there is in memory already a list of directory names and so the executed REN command does not result anymore in a changed list of directory names on processing one after the other.
Please note that a directory with name 01__Underscore_at_beginning is renamed to Underscore_at_beginning and not to _Underscore_at_beginning by both single line solutions.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
ren /?
set /?
setlocal /?
I want to create a batch file able to apply some processing on each JPG file in a folder hierarchy. The following script file works very well for that case (here I only echo the name of each file, but this should be replaced by some more complex statements in the real application):
:VERSION 1
#echo off
set "basefolder=C:\Base"
for /r %basefolder% %%f in (*.jpg) do echo %%f
Actually, I don't want to explore all the folder hierarchy under %basefolder%, but only a given list of subfolders. This modified script is able to deal with that case :
:VERSION 2
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
pushd %basefolder%\%%~s"
for /r %%f in (*.jpg) do echo %%f
popd
)
Is there a solution to remove the pushd/popd pair of statements, to get something closer to the initial script. I thought that one of the following scripts would do the job:
:VERSION 3
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
for /r %basefolder%\%%~s" %%f in (*.jpg) do echo %%f
)
or, using delayed expansion:
:VERSION 4
#echo off
setlocal enabledelayedexpansion
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
set "folder=%basefolder%\%%~s"
echo !folder!
for /r !folder! %%f in (*.jpg) do echo %%f
)
but none of them is working. When running the second one, the echo !folder! command in the external loop shows C:\Base\A, C:\Base\B and C:\Base\C as expected, but the inner loop doesn't echo any JPG file, so I guess that the recursive for /r command does not run correctly.
What am I doing wrong ?
Final edit after answers :
Thanks to #aschipfl who provided a link to the answer posted by #jeb on another question, quoted below:
The options of FOR, IF and REM are only parsed up to the special character phase. Or better the commands are detected in the special character phase and a different parser is activated then. Therefore it's neither possible to use delayed expansion nor FOR meta-variables in these options.
In other words, my versions 3 and 4 do not work because when defining the root folder of the FOR /R command, neither the %%~s nor the !folder! are correctly expanded by the expression parser. There is no way to change that, as this is a parser limitation. As I said in a comment below: the root folder option in the FOR /R command is basically only syntactic sugar to avoid the use of pushd/popd before and after the command. As this syntactic sugar is incomplete, we have to stick to the original syntax for some specific use cases, as the one presented here. The alternatives proposed by #Gerhard (using a subroutine CALL) or by #Mofi (parsing the result of a DIR command) are working, but they are neither more readable nor more efficient than the simple pushd/popd version I proposed initially.
My Approach for this would be really straight forward:
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do for /R "%basedir%" %%a in ("%%~i\*.jpg") do echo %%~fa
The double quotes inside of the subfolders variable is important here, it will ensure that folder names with whitespace are not seen as separators for the folder names. For instance:
set "subfolders="Folder A","Folder B","Folder C""
Edit
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do call :work "%%~i"
goto :eof
:work
for /R "%basedir%\%~1" %%a in (*.jpg) do echo %%~fa
It is in general not advisable to assign the value of a loop variable to an environment variable and next use the environment variable unmodified without or with concatenation with other strings being coded in batch file or defined already above the FOR loop within body of a FOR loop. That causes just problems as it requires the usage of delayed expansion which results in files and folders with one or more ! are not correct processed anymore inside body of the FOR loop caused by double parsing of the command line before execution, or command call is used on some command lines, or a subroutine is used called with call which makes the processing of the batch file much slower.
I recommend to use this batch file for the task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "basefolder=C:\Base"
set "subfolders=A B C "Subfolder D" SubfolderE"
for %%I in (%subfolders%) do for /F "delims=" %%J in ('dir "%basefolder%\%%~I\*.jpg" /A-D /B /S 2^>nul') do echo %%J
endlocal
The inner FOR loop starts for each subfolder defined in subfolders in background one more command process with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is with Windows installed to C:\Windows for example for the first subfolder:
C:\Windows\System32\cmd.exe /c dir "C:\Base\A\*.jpg" /A-D /B /S 2>nul
The command DIR searches
in specified directory C:\Base\A and all it subdirectories because of option /S
for files because of option /A-D (attribute not directory) including those with hidden attribute set
matching the pattern *.jpg in long or short file name
and outputs to handle STDOUT of background command process just the matching file names because of option /B (bare format)
with full path because of option /S.
The error message output by DIR on nothing found matching these criteria is redirecting from handle STDERR to device NUL to suppress it.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The output to handle STDOUT of background command process is captured by FOR respectively the command process which is processing the batch file. FOR processes the captured output line by line after started cmd.exe terminated itself. This is very often very important. The list of files to process is already in memory of command process before processing the first file name. This is not the case on using for /R as this results in accessing file system, getting first file name of a non-hidden file matching the wildcard pattern, run all commands in body of FOR and accessing the file system once again to get next file name. The for /R approach is problematic if the commands in body of FOR change a file to process like deleting, moving, modifying, copying it in same folder, or renaming a found file because of the entries in file system changes while for /R is iterating over these entries. That can easily result in some files are skipped or some files are processed more than once and it could result also an endless running loop, especially on FAT file system like FAT32 or exFAT. It is never good to iterate over a list of files on which the list changes on each iteration.
Command FOR on usage of /F ignores empty lines which do not occur here. A non-empty line is split up into substrings using a normal space and a horizontal tab as string delimiters by default. This line splitting behavior is not wanted here as there could be full qualified file names containing anywhere inside full name one or more spaces. For that reason delims= is used to define an empty list of delimiters which disables the line splitting behavior.
FOR with option /F would also ignore lines on which first substring starts with ; which is the default end of line character. This is no problem here because of command DIR was used with option /S and so each file name is output with full path which makes it impossible that any file name starts with ;. So the default eol=; can be kept.
FOR with option /F assigns by default just first substring to specified loop variable as tokens=1 is the default. This default can be kept here as splitting the lines (full file names) into substrings is disabled already with delims= and so there is always the full file name assigned to the loop variable.
This example uses just echo %%I to output the file names with full path. But it is now safe to replace this single command by a command block which does more with the JPEG files because of the list of JPEG files for each specified subfolder tree in base folder is always already completely in memory of command process processing the batch file.
I have a list of csv files with date and time appended like "Account_data_yyyymmdd.csv" which are added daily along with its timestamp to source dir .I have to identify latest file ie.'Account_data_2020_08_05.csv' and set the value in variable . so i can pass it as argument
Files in source dir
Account_data_2020_08_05.csv
Account_data_2020_08_04.csv
Account_data_2020_08_03.csv
I have to find the recently placed file based on its timestamp & pass it as input for calling another batch process. Highlighted text is the argument to batch file.How to find latest file based on its timestamp and pass it as argument for
echo "start"
call process.bat "C:\CSVDataLod" AccntDataloadprocess ***"dataAccess.name=C:\SourceDir\ Account_data_%year%_%month%_%date%.csv"***
That's surprisingly easy. Use dir with the /on switch to sort by name (see dir /? for that switch and the others I used, if you are not familiar with them) and put a for /f loop around to capture the output. The following code sets the variable %last% to each line of the output, keeping the last one only:
for /f "delims=" %%a in ('dir /a-d /on /b Account_data_*.csv') do set "last=%%a"
echo %last%
The easiest and fastest method to get name of CSV file with newest date in file name is using command DIR with option /O-N to get the CSV file names output ordered by name in reverse order. The file name with newest name is output first by DIR in this case. The output of DIR has to be captured and processed with FOR. The FOR loop is exited after running the other batch file with first file name output by DIR.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileFound="
set "FileNamePattern=Account_data_20??_??_??.csv"
if /I "%~x1" == ".csv" set "FileNamePattern=%~nx1"
for /F "delims=" %%I in ('dir "C:\SourceDir\%FileNamePattern%" /A-D /B /O-N 2^>nul') do (
echo Processing file %%I ...
call process.bat "C:\CSVDataLod" AccntDataloadprocess "dataAccess.name=C:\SourceDir\%%I"
if /I not "%~1" == "/A" goto EndBatch
set "FileFound=1"
)
if not defined FileFound echo There is no file "%FileNamePattern%" in directory "C:\SourceDir".
:EndBatch
endlocal
I recommend to open a command prompt and run
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /O-N
Then you know which lines are processed by FOR. Next run
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /ON
to see how DIR outputs the CSV file names without specifying a specific order resulting in printing the file names as returned by the file system and explicitly ordered by name in alphabetical order instead of reversed alphabetical order.
The file system NTFS returns a list of file names matched by a wildcard pattern in local specific alphabetic order while FAT file systems like FAT16, FAT32, exFAT return the file names not ordered at all. In real all file systems return the file names in order as stored in the table of the file system. The file systems use just different methods on how to add a file name to table of the file system. The FAT file systems append a new file name always at end of the table of a directory while NTFS inserts a new file name in table of a directory using a local specific alphabetic sort algorithm.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.
Edit:
The batch file can be run with /a or /A as argument to process all CSV files matching the wildcard pattern from newest to oldest instead of just the newest. The batch file can be also run with name of a .csv file in source directory to process this specific CSV file instead of the newest CSV file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
dir /?
echo /?
endlocal /?
for /?
goto /?
setlocal /?
I am trying to write a batch script to rename multiple folders.
I would like to do something like below:
Rename all folders under the "Workspace" folder by appending my name in the end of the folder names
For example, rename:
Workspace/RiskFolder
Workspace/PNLFolder
to:
Workspace/RiskFolder_myname
Workspace/PNLFolder_myname
Is this possible?
You could use for to loop through each directory and rename it like so:
for /D %%f in (C:\path\to\Workspace\*) do rename "%%f" "%%~nxf_myname"
I tested this on Windows 7, but it should work at least as far back as with Windows XP.
What that does is this: for each directory in the path (within parenthesis), assign the directory name to the variable %%f, then rename the directory %%f to the name in the format you want (with your name attached). %%f holds the full pathname, which is fine for the first argument to the rename command, but for the second argument, we only want the filename+extension, thus the ~nx modifier prepended to our variable name.
By the way, when using this for loop on the command line (rather than part of a batch file) you only want to use one % instead of %% for your variable name. E.g. for %f in... instead of above.
See the following references from Microsoft for more details:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true
You can use the following command within your batch file:-
for /F "usebackq tokens=*" %%a in (`dir /ad /b %1`) do ren %1\%%a %%a%2
This is the DOS 'for' command, which iterates over given set of items, and for each element in the set, performs the given action. For the given requirement, we need to do the following:-
1) Accept name of folder which contains sub-folders to be renamed(in your example, it is Workspace).
2) Accept the string to be appended to the end(in your example, it is your name).
3) List the names of sub-folders in the folder.
4) Rename by appending the string to original name.
Let's see how this for command accomplishes that. The format of 'for' command used here is:-
for /F ["options"] %variable IN (`command`) do command [command-parameters]
The command here assumes that the required parent directory name and string to be appended are passed on as command line parameters. These are represented by %1 and %2 (first and second parameters).
To enable us to issue a dos command to be evaluated, we need to use the /F option. The option string is :-
"usebackq tokens=*"
usebackq specifies backquouted string is a command to be evaluated.(Note that the dir command is enclosed within backquotes(`) )
tokens=* means to consider each line as a single token and pass to the command
To list the sub-directories in parent directory, we use the command:-
dir /ad /b %1
/ad displays only directories (ignores files)
/b displays it in bare format, i.e., only names are returned and date, time and other info are not.
%1 is the command line variable referring to parent directory.
%%a is the variable which receives the sub-directory name in each iteration. Double percentage symbol is required since we use it in a batch file, otherwise, just one is required (like %a)
Finally, we specify the action to be performed:-
ren %1\%%a %%a%2
%1\%%a constructs absolute path to sub-directory
%%a%2 append second command line parameter to original name
For more info on for command, type following in a command prompt:-
for /?
For another usage example, refer Loopy loops: The DOS way
No need for a batch file. This will work from the command line
for /d %D in ("Workspace\*") do ren "%D" "%~nxD_myName"
If you do use a batch file, then %D must become %%D
If there is no need to perform folder/file renaming with a batch file, you can also use the Bulk Rename Utility for Windows. Check this: http://www.bulkrenameutility.co.uk/Download.php
For /D %%f in (*) do rename "%%f" "%%fWhatEverNameYouLike"
pause
The pause is to see it! Make a cmd of it and put it in the folder that you want to rename all it's subfolders! They'll rename to each one folders name, plus the WhatEverNameYouLike
How would I go about reading an entire directory and blanking files with a specific extension? I have an application that reads the content of a specific folder and returns an error if a file is missing, however it does't check to see if the files are valid, so I want to make them NULL to get around the checks.
if by 'blanking' you mean truncating them, you could use the following:
for /f %%a in ('dir *.[my ext]') do (echo . > %%a)
note that the double % is for use within a batch file. if you are running this from a command line, use a single %.
EDIT:
to incorporate #Loadmaster's improvement:
for /f %%a in ('dir *.[my ext]') do (type nul > %%a)
First, create an empty file. Call it "blank". You can use Notepad to just save an empty file, for example.
Let's suppose the specific extension is ".xyz". Run this:
for %f in (*.xyz) do copy /y blank %f
The for loop sets variable "%f" to each file name in turn, and runs the copy command. The copy command copies the blank file on top of each matching file.
By the way, you can find out more about the for command using the help command:
help for
You could make the batch file take the filter and then it's much more useful.
#for %%i in (%1) do cd.>%%i
Usage (if you call it empty.bat):
empty *.c