I currently have a windows folder with only two files in it:
vacation.jpg
and
summer.avi
What I want to to is rename summer.avi in vacation.avi by using the text that is before the .jpg extension.
So for example if I have another folder with christmas.jpg and summer.avi the .avi file would be renamed into christmas.avi.
Any help would be much appreciated!
You can use the ~n parameter extension to grab just the filename without the extension. The script needs to be in the same directory as the jpg and avi, but if you want to run it on multiple directories, you can wrap the whole thing in a second for loop.
#echo off
setlocal enabledelayedexpansion
:: This assumes there is only one avi file in the folder
for /F %%A in ('dir /b *.jpg') do (
set basename=%%~nA
ren *.avi !basename!.avi
)
This should worlk, though I haven't tested it:
#echo off
for %%i in (*.jpg) do set src=%%i
ren *.avi %src%
Related
I have about a few thousand files in a folder. Each filename has a few different version's of the file so for example. tree.blue, tree.green, tree.orange, cat.black, cat.green,cat.white.
what i would like to do is have a .bat file to put all tree or any word before the "." to auto create a directory and add any file related to tree for example in it as well as for all the other filenames without specifying the word.Currently I have found code on here which almost does it but have to specify the keyword in the code for it to work.
Assuming each file is in the same format, something like this would work:
pushd "path_to_folder"
for /f "tokens=1* delims=." %%g in ('dir /b') do (
if not exist "%CD%\%%g" #echo md "%%g"
#echo copy "%CD%\%%g.%%h" "%CD%\%%g\%%g.%%h"
)
popd
You can remove the #echo once you confirm the system will be creating the right directory name and copying the files to the right location.
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 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 lot of (50 000+) files in a folder. They are named as: "abc_012345678_abcabc.ext"
I need to create a directory for each of this file (all in one different directory), named as the "number part" (012345678) and then copy this file to this directory.
So in the end, i have 50000+ directories, called like "265415873" , "654879623" and so on and inside each directory is the filename with matching name (so in dir 265478951 is file called abc_265478951_abcabc.ext).
Big thanks for any advice!
EDIT:
I already created a simple script, that does what I need, so if anyone is interested, here it is:
#ECHO OFF
setlocal enabledelayedexpansion
FOR %%f in (*.ext) DO (
set filename=%%f
set folder=!filename:~0,-4!
set copypath=e:\work\programy\adresare_handa\adresare\!folder!
mkdir !folder!
copy !filename! !copypath!
)
pause
In this case, I only delete the extension and use the whole name of the file for the name of the directory, but I know, how to alter it, to read just the part of the file I need.
Thanx a lot again, if anyone have any comment for my version (pros/cons), please, do enlighten me :)
Bye!
#echo off
pushd "C:\some_dir"
for /f "tokens=1,2,3 delims=_" %%A in ('dir /b /s /a-d') do (
md "%%~B"
copy %%A_%%B_%%C %%B
)
only in case all files are with name pattern like "abc_012345678_abcabc.ext"
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