I have identical files in four folders (named "1", "2", "3", "4") and would like to copy these files into a single folder, with the original folder name appended to the filename.
E.g. a file called "data.txt" in each folder should be copied to a new merged folder, with filenames such as "data 1.txt" "data 2.txt" etc.
Here is what I have so far, but I never formally learned batch scripting (and can't find any decent tutorials - recommendations please?) and can't seem to make it work. Hopefully this gives an idea of what I want to accomplish.
DIR="$( dirname "$0" )" && pwd )" // I don't understand this but was told it's
// necessary to set the working directory as
// the current folder? Is that correct?
md "consolidated files"
for %%i in ("1","2","3","4") do
copy *.txt '../consolidated files/"*"+%%i.txt'
Any tips for a beginner? Thanks!
#ECHO OFF
SETLOCAL
PUSHD "U:\sourcedir"
MD "consolidated files" 2>nul
for %%i in ("1","2","3","4") DO (
FOR /f "delims=" %%m IN ('dir /b /a-d ".\%%~i\*.txt"') DO (
copy ".\%%~i\%%m" ".\consolidated files\%%~nm %%~i%%~xm"
)
)
popd
GOTO :EOF
Your attempt to set dir appears to be a bash command - useful on *nix but no good on CMD.
Essentially, you can set the current directory using cd
cd "c:\your\desired directory"
Quirkishly, the quotes in that particular command are actually unnecessary (but do no harm, so I put them in.)
Another approach is
pushd "c:\your\desired directory"
rem commands following have current directory "c:\your\desired directory"
rem
popd
rem current directory reestored to value before the "pushd"
I've used the second approach in the above script to switch temporarily to my test directory U:\sourcedir
Note that cmd uses \ as a directory-separator and / as a switch-indicator.
The md command is as you had it. The directory is created relative to the current directory unless the path is specified (eg md "C:\somewhere new"). The2>nulsuppresses thedirectory already exists` message should the directory er, already exist.
in a for...do statement, either the target operation must be on the same line as the do or the do must be followed by Space( and then each statement until a matching ) is executed as a compound statement.
The for..%%i statement assigns the values "1".."4" (including the quotes) to %%i The quotes are actually not required in this case - they only need to be there if the required string includes a Space (or other separator.)
The next command is best understood from the middle. The dir command looks in ".\%%~i\" for files named *.txt. ~i means "remove quotes from %%i". The /b switch shows just filenames - no size, date or header/footer. The /a-d switch says 'no directories'.
This dir command is within single-quotes. FOR /f ...('single-quoted command')... processes the result of the command as if it was a file, line-by-line. The "delims=" suppresses the default tokenising of the strings found, so overall, the filenames found by the dir are assigned to %%m in their entirity.
The command then executed is the copy, copying from ".\%%~i\%%m" (ie. the current directory++the subdirectory(-quotes)++filename; all quoted in case of spaces) to ".\consolidated files\%%~nm %%~i%%~xm" (ie. the current directory+\consolidated files+the name part of the filename (%%~nm)+Space+the subdirectory(-quotes)+the extension part of the filename (%%~xm))
Note that + is a valid filename character (as is ') and that strings are built simply by being butted up one against the next.
Your original question stated that the source directoryname should be appended after a space, hence I've included the space.
Note that copy will report 1 file(s) copied after each copy. You can suppress this by adding >nul to the end of the copy statement.
For testing, I would change copy to echo copy which will show the command generated but not execute it. Unfortunately, if you have the >nul in place, the echo of the command will be suppressed...
Related
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 was trying to move my files(books) based on it's author name.
For example:
[author] Title 1.pdf
[author2] Title A.pdf
I've found a batch script for this
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
It works but it made each folder for each files, what I want to do is create folder by author names and move it there.
Note: All author name have "[]" in it's file name but the folder created only has author name without "[]".
Please help, I have 4000+ files I need to sort.
The following script uses a for /F loop to split the file names which are gathered by the dir command:
#echo off
for /F "tokens=1* delims=[] eol=]" %%I in ('dir /B /A:-D-H-S "[*]*.pdf"') do (
ECHO md "%%I" 2> nul
ECHO move "[%%I]%%J" "%%I\"
)
The 2> nul portion suppresses error messages in case the directory to create already exists.
After having tested for the correct output, remove the upper-case ECHO commands from the md and move command lines. To avoid multiple 1 file(s) moved. messages, append SPACE + > nul to the move command line.
The trailing \ at the destination of the move command is intended to force it to point to a directory. Imagine the destination directory could not be created for some reason (for example, lack of access privileges), the destination without the \ is interpreted as a new file name in the working directory, leading to unintentional renaming; with the \ the move command expects an existing directory, and if not found an error arises.
Besides the fact that your code did not attempt to split the file names as needed, there is one additional problem: the && operator lets the following command execute only in case of success of the former one; so when md failed, move does not run. For example, you have got two files with the same author, so when iterating the second one, the destination directory already exists since it has been created in the previous iteration, so md fails and the respective file is not moved. Therefore you should have used the unconditional & operator.
I need to get the name of the directory for each file with extension *.txt within specified folder/directory (including its subfolders/subdirectories).
I've tried the solution from here: Parent folder from full path
But it looks like that set "myPath=%%~Pa" doesn't do anything in the code below:
#echo off
cd /d "F:\Game\"
for /r %%f in (*.txt) do (
echo file: %%f
set "myPath=%%~Pa"
echo myPath: %myPath%
for %%a in ("%myPath:~0,-1%") do (
set "myParent=%%~Na"
echo %myParent%
)
)
pause.
For these files:
F:\Game\file1.txt
F:\Game\first_subfolder\file2.txt
F:\Game\first_subfolder\hellothere\file3.txt
F:\Game\first_subfolder\hellothere\lala\file4.txt
I expect to get the name of the directory for each of them:
Game
first_subfolder
hellothere
lala
How to get the name of the directory for each text file in directory tree?
I suggest first opening a command prompt window, run set /? and read the output help from top of first page to bottom of last page, especially the section about delayed environment variable expansion. See also: Variables are not behaving as expected.
But it is possible to get the directory name for each *.txt file in directory tree without using delayed expansion.
#echo off
for /R %%I in (*.txt) do for %%J in ("%%~dpI.") do (
echo Text file: %%I
echo Directory: %%~nxJ
)
Note: %%~nxJ is an empty string if this batch file is executed from root of a drive and there is a *.txt file in root of this drive.
The trick here is %%~dpI expands to full path of current text file ending with a backslash. . is concatenated to this path string to reference the folder. Then %%~nxJ expands to name of the folder without path. See also the Microsoft documentation page Naming Files, Paths, and Namespaces.
I'm using the following batch code to convert all files in a certain directory if the target file doesn't already exist however I'm stuck at getting this to run through every submap and file within that (and keep the output relative with that submap)
So I currently use this:
for %%f in (input/textures/*.*) do ( IF NOT EXIST "ouput/textures/%%~nf.dds" (
"bin/ThempImageParser.exe" "input/textures/%%f" "ouput/textures/%%~nf.dds"
)
)
This works perfectly for a single folder (as was intended), it takes all the files in that specific folder, and passes them as arguments to my executable, which then outputs the file on the path of the second argument.
However this also contains a flaw (this is an additional problem though..) as it does not work if the output -folder- does not exist, so if possible I'd also want it to create the folder if need be.
I've found some batch documentation (I really don't have much experience with Batch) showing me a command called FORFILES and the /R parameter, however I couldn't adjust this so it'd keep the relative paths for the output too, it'd require string manipulation and I have no clue on how to do that.
So the result I'm after is something like this, it takes any file deeper than "input/textures/ for example:
input/textures/some/very/deep/submap/why/does/it/go/on/myfile.anyExtension
it should then take that file (and relative path) and basically change "input" with "output" and replace the file extension with .dds like this:
ouput/textures/some/very/deep/submap/why/does/it/go/on/myfile.dds
and pass those two strings to my executable.
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir\wherever\something"
FOR /f "delims=" %%a IN ('xcopy /y /L /s "%sourcedir%\*"') DO (
SET "destfile=%%a"
SET "destfile=!destfile:*%sourcedir%=%destdir%!"
IF /i "%%a" neq "!destfile!" (
FOR %%m IN ("!destfile!") DO IF NOT EXIST "%%~dpm%%~na.dds" (
ECHO MD "%%~dpm"
ECHO "bin\ThempImageParser.exe" "%%a" "%%~dpm%%~na.dds"
)
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
First, perform an xcopy with the /L option to list-only the individual fullnames of files that would be copied by the xcopy.
Assign each name found from %%a to destfile, then remove all characters before the source-directoryname from that filename, and replace that string with the destination directoryname.
This will yield the destination name for the file (with the original extension). The only exception will be the very last output line, which is a count-of-files report. Since this line will not contain the source directoryname, the replacement will not take place, so %%a will be the same as !destfile! - so we eliminate that.
Now assign the destination filename to a metavariable so we can select its various parts, and if the filename made from the destination drive and pathname, the name part of the original file and .dds does not exist, then make the destination directoryname and execute the imageparser, providing the desired output filename.
Note that these last two are ECHOed instead of being executed for testing purposes. Remove the ECHOes to actually perform the command.
Note that / is a switch-indicator, \ is a directory-separator.
Note that MD will report an error if the directory already exists. Append 2>nul to the end of the md command to suppress that error message.
I am very new to coding and bulk processes but i am looking for a command line SPECIFICALLY for windows command prompt and i am wondering if such a thing exists. So I have a folder containing 111 subfolders, with each subfolder containing between 20 and 40 png image files. Each subfolder is named 001-111 accordingly and the png files are ordered how i want them, however i am looking for a command line that would be able to quickly and efficiently name all the pngs in the folders to the name of the folder followed by the png number in brackets
e.g. for folder 037, i would want the png's to be renamed to: 037(1), 037(2), 037(3) etc...
I am hoping for the best although i am unsure such a code may not be possible or be simply done.
Also if you come up with a code that achieves this process, it would be great if you could reply with the simple command line that i could use rather than a full explanation because i am new to coding and far from fluent with the language or terms or how things work. I know this same process can be achieved by going select all>rename (ctrl a>f2) and renaming to the folder name however i need to use this process frequently and dont want to have to open each folder, i would rather have a command line for cmd that would do it swiftly
Thank you and a simple answer would be greatly appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "parentdir=u:\parent"
FOR /l %%a IN (1001,1,1111) DO (
SET dir=%%a&SET "dir=!dir:~1!"
FOR /f "delims=" %%i IN ('dir /a-d /b "%parentdir%\!dir!\*.png" 2^>nul') DO (
ECHO REN "%parentdir%\!dir!\%%~nxi" "!dir!(%%~ni)%%~xi"
)
)
GOTO :EOF
Test results:
Starting directory :
u:\parent\001\1.png
u:\parent\037\1.png
u:\parent\037\2.png
u:\parent\111\999 with spaces in name.png
Script response
REN "u:\parent\001\1.png" "001(1).png"
REN "u:\parent\037\1.png" "037(1).png"
REN "u:\parent\037\2.png" "037(2).png"
REN "u:\parent\111\999 with spaces in name.png" "111(999 with spaces in name).png"
Obviously, you'd need to replace the value assigned to parentdir with your actual target directory name.
The script will report the renames it proposes to do. To actually invoke the rename remove the ECHO keyword.
I would create a batch file like so:
renamepng.bat:
cd %%1
if ERRORLEVEL 1 goto end
for %f in *.png do mv "%f" "%%1(%f).png"
cd ..
:end
This will attempt to cd to the directory name provided on the command line, abort if that fails, then rename all the .png files and return to the previous directory
then call it like so:
for %d in ??? do call renamepng.bat %d
which will loop through all 3-character file and directory names in the current directory, can call the batch file on each one. Using call instead of just the batch file name causes execution to return to the loop when the batch finishes.