Batch script for loop with prefix - batch-file

I've written a batch script which processes a series of files, but now I want it to copy the output files to the correct directory.
for %%f in (closecaption_*.txt) do (
"%cd%\bin\captioncompiler.exe" "%cd%\%%f"
copy /y "%cd%\%%f" "Z:\Users\jbzdarkid\"
)
pause
Currently, this script will copy all of the .txt files into my home directory. However, I want it to move the .dat files (the output) into my directory instead. Ideally, it would only move the new .dat files, i.e. the ones that have just been processed.
I would like to be able to do something like this:
for %%f.txt in (closecaption_*.txt) do (
"%cd%\bin\captioncompiler.exe" "%cd%\%%f.txt"
copy /y "%cd%\%%f.dat" "Z:\Users\jbzdarkid\"
)
pause
however this does not work.

for %%f in (closecaption_*.txt) do (
"%cd%\bin\captioncompiler.exe" "%cd%\%%f"
copy /y "%cd%\%%~nf.dat" "Z:\Users\jbzdarkid\"
)
pause
should work, if my crystal ball is working correctly. It selects the name-par of %%f.
Some examples would have made the matter clearer.

The following code should work:
for /f "tokens=1,2 delims=." %%f in ('dir /a-d /b "%cd%\closecaption_*.txt"') do (
"%cd%\bin\captioncompiler.exe" "%cd%\%%f"
copy /y "%cd%\%%f.dat" "Z:\Users\jbzdarkid\"
)
pause

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 ...

Script batch copy from subfolder to main folder filtering extension

I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied

Batch file moving to subfolders

I have lots of folders including images in it. Example:
C:\U2090_08
C:\U2111_08
C:\U2024_03
C:\U2024_08
C:\U2049_15
C:\U2049_35
There are 3-4 jpg files in every folder. I want to create a sub-folder called "kck" in every folder and move jpg files to this subfolder.
Example:
Before process:
C:\U2049_35\1.jpg
C:\U2049_35\2.jpg
C:\U2049_35\3.jpg
After process:
C:\U2049_35\kck\1.jpg
C:\U2049_35\kck\2.jpg
C:\U2049_35\kck\3.jpg
Here is what I am trying:
#echo off
cd %USERPROFILE%\Desktop
:: Sorting images in '\Desktop\images'
for /f "delims=" %%I in (' dir /b "%USERPROFILE%\Desktop\images\*.jpg" ') do (
if not exist "%USERPROFILE%\Desktop\images\%%~nI\kck" ( md "%USERPROFILE%\Desktop\images\%%~nI\kck" )
move "%USERPROFILE%\Desktop\images\%%~I" "%USERPROFILE%\Desktop\images\%%~nI\kck\"
)
exit
Folders including jpg files in a folder called images on desktop.
Any ideas ?
Advanced Renamer software is able to do this. I have just tried it and it have very good move options. It solved my problem.
Next code snippet could help:
#echo off
pushd "%USERPROFILE%\Desktop\images"
:: Sorting images in '\Desktop\images' (subfolders only)
for /f "delims=" %%I in (' dir /b /A:D ') do (
if exist "%%~nxI\*.jpg" (
md "%%~nxI\kck" 2>NUL
move "%%~nxI\*.jpg" "%%~nxI\kck\"
)
)
popd
exit
all paths used in dir, md and move commands are relative to the current directory which is changed to "%USERPROFILE%\Desktop\images" via the pushd command;
note 2>NUL: redirected STDERR stream in the md "%%~nxI\kck" 2>NUL command instead of testing created folder existence explicitly by if not exist "%%~nxI\kck\NUL" md "%%~nxI\kck".

bulk rename files within a folder to parent folder name

I have a large (aprox. 150,000) tif files, which all have the same filename. They are only unique because of the directory structure they are held in.
I would like to bulk rename the tiff files so they become unique, based on the directory structre that they are held within.
Does anyone have any method of acheiving this?
I am using Windows Server 2012 so a solution using a cmd script, batch file or windows GUI tool would be perfect.
Ideally, this is what I would like to acheieve, but if I have to have more or all of the directory structure in the final filename thsi would still be very, very helpful.
C:\A_001\B_0001\ABC\0001.tif -> ABC.tif
C:\A_001\B_0001\JKL\0001.tif -> JKL.tif
C:\A_001\B_0001\XYZ\0001.tif -> XYZ.tif
C:\A_001\B_0002\123\0001.tif -> 123.tif
C:\A_001\B_0002\456\0001.tif -> 456.tif
C:\A_001\B_0002\789\0001.tif -> 789.tif
This should work:
pushd C:\A_001\B_0002
for /d %%a in (*) do (
if exist "%%~a\0001.tif" ren "%%~a\0001.tif" "%%~a.tif"
)
popd
Which should do what you want. I've tested this and it works fine on my computer.
i made a similar foldertree on my drive F:.
Start a commandprompt from within folder "A_001"
FOR /F "TOKENS=3,4,5* DELIMS=\" %A IN ('DIR "%CD%\*.tif" /s /b /a:-d') DO MOVE "%CD%\%A\%B\%C" "%CD%\%A_%B%~xC"
Will result in:
MOVE "F:\A_001\B_0001\123\0001.tif" "F:\A_001\B_0001_123.tif"
MOVE "F:\A_001\B_0001\ABC\0001.tif" "F:\A_001\B_0001_ABC.tif"
MOVE "F:\A_001\B_0002\123\0001.tif" "F:\A_001\B_0002_123.tif"
MOVE "F:\A_001\B_0002\ABC\0001.tif" "F:\A_001\B_0002_ABC.tif"
So you'll have all your files in the folder "A_001".
Hope this helps?
#echo off
for /f "delims=" %%F in ('dir /b /s /a-d "C:\A_001\0001.tif"') do (
for %%D in ("%%~dpF.") do ren "%%F" "%%~nD%%~xF" || echo unable to rename "%%F"
)

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.

Resources