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
Related
I created a .bat file with the following code in order to copy a random file from a directory A-POOL FOLDER into a directory B-FOLDER (with a fixed file name video.mp4). So, in the directory A there is a pool of .mp4 file, and in the directory B there is the the file video.mp4 (same name, but different video every time I execute my batch-file).
That's a code who do this. It perfectly works.
#echo off
setlocal EnableDelayedExpansion
cd C:\Users\aless\Desktop\example\A-POOL
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" C:\Users\aless\Desktop\example\B-FOLDER\video.mp4
FOR %%G IN ("!file[%rand%]!") DO >"C:\Users\aless\Desktop\example\B-FOLDER\title.txt" ECHO %%~nG
Now the question (different from last time):
In the directory A-POOL I have a lot of .mp4 files. I changed manually the .mp4 extension of every file with a code who rappresent a food.
For example: Filename.pizza, Filename2.pizza, Filename.pasta, Filename200.pasta, Filename.cheeseburger etc...
I created another folder named "FOOD-DATABASE" (in C:\Users\aless\Desktop\FOOD-DATABASE) who contains a big database of images in .jpg extension of the foods. The file NAMES of the food database are equal to the file EXTENSIONS of the files contained in A-POOL folder
Content of FOOD DATABASE folder:
(pasta.jpg, pizza,jpg, cheeseburger.jpg... etc..).
So I need to add to the code a string/conditional who execute e command who say:
If the picked random File have the extension .pizza COPY pizza.jpg from C:\Users\aless\Desktop\FOOD-DATABASE into C:\Users\aless\Desktop\B-FOLDER\FOOD.JPG (fixed name file)
Actually I don't want to add this strings for every food type... in few word the code strings have to read the extension of the picked random file and copy from C:\Users\aless\Desktop\FOOD-DATABASE to C:\Users\aless\Desktop...B FOLDER\FOOD.JPG (fixed name file) the file with the same name to the extension of the random file.
It's a mess... I know ;) Thanks for your help!
As mentioned in the comments, we know that the extension of the one file is the name of another file. so these variable expansions are important:
%%~nG
%%~xG
The last for loop sets a variable %food% from expanded %%~xG which is the extension of the file and using a random example would be .pizza We just need to get rid of the . and you have pizza. We do this by %food:~1.
#echo off
setlocal EnableDelayedExpansion
pushd "%userprofile%\Desktop\example\A-POOL"
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" "..\B-FOLDER\video.mp4" /Y
for %%G IN ("!file[%rand%]!") DO (
echo %%~nG > "..\B-FOLDER\title.txt"
set "food=%%~xG"
)
copy "..\FOOD-DATABASE\%food:~1%.jpg" "..\B-FOLDER\food.jpg" /Y
popd
Notes!
I am using pushd instead of cd and using relative path, not full paths as we are working in the same folder structure.
I can make out that you are running this on your own profile, so I replaced C:\users\<username> with %userprofile% in the event someone else wants to run it from a similar folder, their <username> will be different.
popd at the bottom of the script simply changes back to the previous working directory you were in before the last pushd. it is not needed for this particular script, but adding it so you can learn about it compared to pushd
All of the items I modified has some good help topics, you can find help topics on cmd by simply running the command name with an appended /? for instance for /?, set /? etc.
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 ...
I'm a beginner with .bat files, and I'm attempting to rename multiple drawing (.dwg) files using a list generated in notepad. The notepad list contains a list of drawing numbers that look like this:
01013-13000p001
06301-12550p001
etc..
There is hundreds of them, and I want to take those numbers from the text and put it into a blank series of dwg files that are generic named for now (drawing.dwg, drawing(2).dwg, drawing(3).dwg etc..) I've only come up with a way to read a text file, but cant figure out how to take from the text file and rename multiple drawing files with it. Below is as far as I have gotten, after failed attempts of trying to take whats read from a text file and put it into the .dwg files. I plan on working this out in all the same directory, and any suggestions will be greatly appreciated. Thanks.
#echo off
for /f "tokens=* delims=" %%x in (dwgNumbers.txt.txt) do echo %%x
pause
would
for /f "delims=" %%x in (dwgNumbers.txt.txt) do echo copy /b "blank generic drawing.dwg" "%%x.dwg"
(as a batch line) do what you want? - note that the ECHO keyword is there to show what would be done. The echo keyword needs to be removed to actually execute the copy.
This will take the numbers from .txt, renaming the existing .dwg files with the data readed. If there are more files that numbers in .txt, it will rename until number exhaustion, no more.
for loop is using a dir command to get the list of files to avoid the case of files that after being renamed falls under the filter of the for and gets reprocessed.
This code has a echo command included in rename line to prevent data loss in case of malfunction. When the output to console is what is needed, remove the echo command from the rename line.
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem Read input file for numbers
< dwgNumbers.txt (
rem Process dwg files in directory
for /f "tokens=*" %%f in ('dir /b *.dwg') do (
rem Get number from input file
set "number="
set /p "number="
rem If there is a number, rename the file
if defined number echo ren "%%~f" "!number!.dwg"
)
)
rem Clean
endlocal
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
)
I have thousands of files to move.
I have already used a batch file to create the directories I need.
My file names look like this:
6711_05_12.pdf
10504_06_15.pdf
559_07_11.pdf
The first characters up to the "_" are the directory the files need to go into. Started the batch file - but don't know how to identify the file name.
#echo off
setlocal EnableDelayedExpansion
for %%I in (*.pdf) do (
xcopy ???
)
Is there a manual for batch files?
You don't even need a batch file. The following one line command will do the trick.
for %I in (*.pdf) do #for /f "eol=_ delims=_" %A in ("%I") do #copy "%I" "%A"
Simply double up the percents if you want to put the command in a batch file.