I have an issue to rename a file to a result of a dir.
Example :
I have a folder name candidate in C drive and inside candidate folder there is below files :
something.txt
everything.doc
onething.bmp
When I dir , I will get the listing of the files.
In the folder will always be 1 txt file and 1 bmp file
How can I rename the BMP file so it will have the same name as the TXT file assuming that I use dir *.txt
In the example above how can I change onething.bmp to something.bmp
Thanks in advance for any answers
for %%X in ("*.txt") do (
for %%Y in ("*.bmp") do (
echo ren %%Y %%~nX%%~xY
)
)
remove the echo if the output is what you need.
For explanation of %%~nX etc. see for /?
This should work:
#ECHO OFF
FOR %%X in ("*.txt") DO (SET txtName=%%X)
FOR %%X in ("*.bmp") DO (SET bmpName=%%X)
FOR /F "tokens=1 delims=." %%X IN ("%txtName%") DO (
REN %bmpName% %%X.bmp
)
This will only work if the bat file is located in the same folder as the txt and the bmp files. If it's located somewhere else you will have to adjust the path.
If there are several bmp/txt files in the foleder, only the "last" files will be processed.
Related
The files names are a part of the name of the directories, which can be at the end or at the beginning of the directory. For example:
pic.jpg into directory \newyork-pic
flower.gif into directory \italy-flower
computer.jpg into directory \computer-informatic
etc
Do you know a way to do that with a batch file?
Here's a piece of example code which should get you most of the way there:
#Echo Off
For /F "Tokens=1-2 Delims=-" %%A In (
'Dir/B/AD-S "*-*"^|FindStr/BRIE "[^-]*[-][^-]*"') Do For %%C In (
"%%A" "%%B") Do If Exist "%%~C.*" Move /-Y "%%~C.*" "%%A-%%B"
Couldn't find a way to do this, looking to store the names of all zero byte files from a folder in a list.
if exist *.xml for %%i in (*.xml) do if %%~zi==0 ECHO "%%i" >> list.txt
this works but the next step would be to read the list.txt and move all files with the same file name even if it has a different extension, also the other file isn't zero bytes that has the same file name but different extension.
example 12345.html and 12345.xml - both files would be moved because the name is the same.
any help would be amazing!
thanks
look at the modifiers in for /?. For example %%~nA would give you the name without the extension.
for /f "delims=" %%A in (list.txt) do (
echo ---- %%nA ---- files with same basename:
dir /b "%%~dpnA.*"
)
I have a batch file that lists the full file directories (including filename at the end) onto a csv file. I need it to produce this, but also just the filename in a separate column. I would like it so that the format is Filename in first column (including extension) and full file directory in second column. The batch file I currently have is:
dir C:\Users\Administrator\Desktop\test\Files\*.tif /b /s >>
C:\Users\Administrator\Desktop\test\Output.csv
EDIT: I forgot to mention the 'Files' folder contains many subfolders so I need it to process all files from all these subfolders.
Thanks in advance.
You just need to do this:
#echo off
setlocal
set "in=C:\Users\Administrator\Desktop\test\Files\*.tif"
set "out=C:\Users\Administrator\Desktop\test\Output.csv"
if not exist "%out%" type nul>"%out%"
for /f "delims=" %%a in ('dir /b/s %in%') do (
>>%out% echo "%%~nxa","%%a"
)
Iterate and write (help for for info on the %~ variable modifiers):
#echo off
cd C:\Users\Administrator\Desktop\test\Files
for %%f in (*.tif) do echo "%%~nxf","%%~dpf" >> Output.csv
I never wrote batch files before and now I am having a requirement of renaming every file with YYYYDDMM from the filename aaaYYYYMMDD123456.csv
The code below works if there is only one file, but doesn't work if there are multiple files.
for %%F in (aaa*f.csv) do ( set "name=%%F" ren "%%F" "!name:~3,8!.csv" )
In case of multiple files the last file's YYYYMMDD is renamed for the first file and all the remaining files show the error "A duplicate file name exists, or the file cannot be found"
You can try the code below:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for %%F in (aaa*.csv) do (
set name="%%F"
ren "%%F" "!name:~4,8!.csv"
)
Basically, I have a text file with one line of text SetNumber=01 in 5 folders
C:\Documents and Settings\User\Desktop\Test\test.txt
C:\Folder\Test\test.txt
C:\Test\test.txt
etc.
I need to change this SetNumber=01 to different numbers monthly, for instance SetNumber=01 to SetNumber=02, in all these folders, and would like to run a batch file that would copy and replace this line of text from
C:\Documents and Settings\User\Desktop\Test\test.txt
into
C:Folder\Test\test.txt.
etc.
Any help would be greatly appreciated!!
OK, here we go. This copies the file C:\Documents and Settings\User\Desktop\Test\test.txt to all ..\test\test.txt files on the current volume (they were erased). Remove the echo command, if the output is OK:
#echo off&setlocal enabledelayedexpansion
set "sourcefile=C:\Documents and Settings\User\Desktop\Test\test.txt"
for /f "delims=" %%i in ('dir /s /b /a-d \test.txt') do (
set "fpath=%%~fi"
if "!fpath:*test\test.txt=!"=="" if not "%sourcefile%"=="%%~fi" (
echo copy "%sourcefile%" "%%~fi"
)
)
The Batch file below change all files named test.txt in any folder in the disk by inserting this line "SetNumber=%1":
#echo off
for /R \ %%a in (test.txt) do echo SetNumber=%1> "%%a"
For example, if previous Batch file is called SetNumber.bat, you may change all the files to SetNumber=02 with this command:
setnumber=02
Antonio