I can't find any references explaining how to loop through a folder passed as an argument and rename each file. All examples I've seen assume the script is running in the folder where files will be renamed or hardcode the folder path in the loop. How can I do this? Here is an example of what I'm trying to do:
for /f %%a in (%1) do call :RenameFiles
:RenameFiles
Rename %%a "new filename"
Goto :EOF
The following adds prefix "renamed-" to every file in the directory specified as a command line argument:
for %%F in (%~1\*) do ren "%%~F" "renamed-%%~nxF"
EDIT The simplest solution to the problem of the infinite loop is to work in two stages:
prepare the list of operations
execute the plan.
-
set OpList=%TEMP%\%~n0%RANDOM%.bat
copy nul "%OpList%"
for %%F in (%~1\*) do echo ren "%%~F" "renamed-%%~nxF" >> "%OpList%"
call "%OpList%"
del "%OpList%"
Related
I have ~10,000 files in one folder. I want to copy specific files to specific folders based on a text file. Is it possible to use a semicolon delimited text file where the first part of each line is the source path & file name and the second part is the destination path?
C:\Files\File1.txt;C:\Folder1
C:\Files\File2.txt;C:\Folder2
C:\Files\File3.txt;C:\FolderN\
What would the code look like? Is there a better way to achieve the same result?
I have an existing bat file I use to copy all of the files listed in a text file to one specific folder location (below) but in this case I need to send different files to different folders and I would rather not run my bat file 50 times, changing the destination path in the bat file each time...
for /f %%f in (%1) do (
copy %%f G:\Files\PutFilesHere
)
It looks like this:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "usebackq delims=; tokens=1,2" %%I in (`type filelist.txt`) do (
copy /y %%I %%J
)
goto :eof
The for loop is told to use the result of a command (usebackq, for command type filelist.txt), to split at each ;, and to take elements #1 and #2. The fist is named as the variable (%%I, take care, case sensitive), and the second is the next letter, so %%J.
Then, the copy is trivial.
for /f "tokens=1,2delims=;" %%f in (%1) do ( copy "%%f" "%%g" )
see for /? from the prompt for documentation
I am trying to find specific files within a folder and its sub-folders and copy them to a new folder. I used to use this batch file for this purpose but now I get this error :
The system cannot find the file specified.
Here's the batch-file content:
pushd "\\internal.company.com\path\"
md myfile
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\\internal.company.com\path\myfile"
Any input is appreciated.
UPDATE
I tried printing %%G like this:
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G
and it works well. The problem arises with copy command which cannot read %%G as an argument.
I'd change to
... copy "%%G" ...
which would cater for spaces in %%G.
Perhaps you should determine whether the problem is with the for/r or %%G. If you simply display %%G with an echo %%G command, then you may see some problem. If it still won't run, then the for /r is the problem.
Since you are pushding to the appropriate directory, there is no need to specify the target directory in the for/r. It can be omitted or replaced by .
or perhaps echo %cd% directly after the pushd to show whether the pushd is the cause of the problem.
I realized that this would work for me (executed in cmd). However, it is plausible because I don't have any *.txt file in the main directory. Not the best solution but a workaround.
pushd "\\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\\internal.company.com\path\myfile"
Note: if you want to execute this as a batch file, you need to use %%f instead of %f.
This will copy the files into main folder and then moves them into the desired sub-folder.
I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…
I have a directory with a number of folders that each include sub-folders. I want to run a batch that searches those folders for the existence of a specific sub-folder called "failed". If there is a file present in this sub-folder, I want it to then move that file to another defined folder.
I've tried to look up usage of "if exist" commands but can't seem to find anything that directly suits my needs.
The names and types of the files to be moved are random, which is why I believed the "if exist" to be the most utile.
Any suggestion?
Read help for paying attention to the /r and /d options and then try this one-liner in the command line
for /d /r %a in (failed) do #echo %a
you will see that this loop iterates over all the directory structure and appends failed to the directory name
then add a check for the actual existence of the directory
if exist %a\nul #echo %a
then add a second loop to iterate over all of the contents of the found failed directory
for %b in (%a\*) do #echo %b
then replace the echo with the required move command
move %b %destination%\%~nxb
and there you go, with a simple single line
for /d /r %a in (failed) do #if exist %a\nul #for %b in (%a\*) do #move %b %destination%\%~nxb
or, translated into a bat file and spiced it up a bit
pushd %sourceroot%
for /d /r %%a in (failed) do (
if exist %%a\nul (
for %%b in (%%a\*) do (
move %%b %destination%\%%~nxb
)
)
)
popd
you'll have to figure out a way to rename the destination file to avoid name clashes.
Say I have a folder "C:\pokepoke" with archives like:
trequetry.part01.rar
trequetry.part02.rar
trequetry.part03.rar
and
replokitro.part01.rar
replokitro.part02.rar
replokitro.part03.rar
replokitro.part04.rar
and so on. How can I make it so the first set of archives ends up in C:\pokepoke\trequetry and the second in C:\pokepoke\replokitro and so on. So basically filter on X.partX.rar or something in that direction.
I am trying to batch-create recovery files with par2 for lots of split-up archives so in addition to my main question, I would also like to find out how to perform a for-each on all subfolders in C:\pokepoke so I can run the following code on it once all archives are moved to their respective folders:
FOR /R %%g IN (.) DO C:\WINDOWS\par2.exe c -r10 -s384000 "%%g\%%~ng.par2" "%%g\*"
del /q *.par2
To help you with your first question, the following script worked for me:
#ECHO OFF
SET "origloc=D:\path\to\archives"
FOR %%F IN ("%origloc%\*.part*.rar") DO CALL :process "%%F"
GOTO :EOF
:process
CALL :checkpath "%~dpn1"
MOVE %1 "%subfolder%" >NUL
GOTO :EOF
:checkpath
SET "subfolder=%~dpn1"
IF NOT EXIST "%subfolder%\" MKDIR "%subfolder%"
GOTO :EOF
This script searches for *.part*.rar files in the specified folder. It applies the ~dpn modifier to every name twice to strip the name of the 'double extension' .partNN.rar, then uses the resulting name as the subfolder name.
Optionally you could change the first SET command like this:
SET "origloc=%~1"
to be able to call the script for an arbitrary path, passing the path as a parameter.