batch renaming multiple file names - batch-file

I'd like to rename specific image names from *_PolishedChrome.jpg to be *-pch.jpg. I tried a bunch of different ways, included the rename DOS command, but cannot find the right way to do it. The files are in a folder on my desktop and they are all .jpg images.
There are multiple files with the last part of the image name.
Example: rename 3-04012C_PolishedChrome.jpg to be 3-04012C-pch.jpg
I'd appreciate any help, thanks!

I think this will do what you are looking for (just place it in the same folder as the jpgs and run):
Remember to make a backup of your files before running this, as it does move files on the disk. It could be done with rename, but that is more effort, as I'd have to parse out the file name.
Also note that running this twice will re-rename your files, so it can only really be run once within a folder... that can be changed, but this is more of a starting point.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /r %%i in (*.jpg) do (
set str=%%i
set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
move %%i !str!
)
DEBUG version - prints out a log.txt to see what went wrong.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /r %%i in (*.jpg) do (
set str=%%i
set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
move %%i !str!
#echo move %%i !str! >> log.txt
)

Related

Copy list of files from directory+subfolders to another folder

First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.
Example:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so
what I basically want is: \Tabletop Simulator\Mods\ as source path and
the script shall look into all subfolders, too.
The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
#echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause
#echo off
setlocal
set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
set "file_list=%USERPROFILE%\Desktop\list.txt"
set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
if not exist "%dst_folder%" md "%dst_folder%"
for /d /r "%src_folder%\" %%A in (*) do (
pushd "%%~A" && (
for /f "usebackq delims=" %%B in ("%file_list%") do (
for %%C in (%ext_list%) do (
if exist "%%~B%%~C" (
echo copy /y "%%~B%%~C" "%dst_folder%\"
)
)
)
popd
)
)
You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.
A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.
The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.
If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.
To view help for a command, use command /?. This will work for many of commands used in the code.
View command /? help for copy, for, if, setlocal ...

Output list of matching files to .txt then move files, use txt to later return files to original folder

There is really 2 parts to this question:
I have a folder full of files that I need to temporarily move to a
new location.
However I will later need to move the files back to their original
location(s).
I have a single folder, full of files and folders that looks like this:
C:\VIDEO\My Video 1\My Video 1.mkv
C:\VIDEO\MyVideo2\MyVideo2.mkv
C:\VIDEO\My.Video.3\My.Video.3.mkv
I need to:
1. Recursively find all *.mkv files within C:\VIDEO folder
2. Output a list of the existing Dir structure/file and folder names/paths to a .txt file
3. Then Move all *.mkv files from C:\VIDEO to another folder C:\Temp
(I do not want to retain the original folder structure during this move)
At a later time I then need to:
4. Search for all *.mkv files in C:\Temp
5. Use the .txt file to help move each *.mkv file back into their original location
I guess this will probably require 2 separate batch files.
Here is my current progress:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Setup
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
:: Output Items To Txt File
for /f "delims=\" %%A in ('dir "%SourcePath%"\*.mkv') DO echo "%SourcePath%">>"%DestPath%"\output.txt
:: Move Matching Items
for /f "tokens=*" %%a IN ('dir "%SourcePath%"\*.mkv') DO move /y "%SourcePath%\%%a" "%DestPath%"
Can somebody please help?
You want to move a tree to a flat destination (knowing you later want to move it back and have to recreate the tree)? Why on earth would one do that...
But ok:
#echo off
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
echo #echo off > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
ECHO move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
echo done. To move them back, execute MoveBack.bat
Instead of logging the moved files to a text file and later iterate over that file, it's easier to just build a "restore" script.
For a description of the %%~ modifiers, read the output of for /?
NOTE: I disarmed the move command for security reasons. If you are sure it works as intended, remove the ECHO.
Note: it's possible to have duplicate file names in a folder tree. This script does not account for that (saying: you may lose data in this case)
The final product:
#echo on
set "SourcePath=H:\FIXED"
set "DestPath=H:\Temp"
echo #echo on > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
#echo done. To move them back, execute MoveBack.bat
is now working.

Recursively append folder name to the files in Windows batch file

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…

Windows batch move (or copy) PDF file by name to corresponding folder

I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

Resources