renaming files to a new name folder using batch script - batch-file

Have tried to rename files in a folder with this script, but its seems not to work
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\path to file that contains the listed names"
for /f "tokens=*" %%f in ('dir /b *.txt') do (
SET newname=%%f
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"
)
what am trying to achieve is my script should pick set of listed names in a file and rename each file in the specified folder accordingly

test this script
#echo off
set prefix=new
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir *.txt /b') do (
set name=%%~Na
set newName=%prefix%!name:~0,1!X!name:~1,2!!name:~3!
ren "%%a" "!newName!%%~Xa")

First you said you want to rename each file "accordingly" (accordingly to what?), and later in a comment you said you try to rename files "with a set of listed names in a file". This point cause several additional questions: Have this file one name in each line? Must the first file listed by dir /b *.txt match the first name listed in the file, and so on? Any other option? (Why do you use a move command to do a "rename"?).
Because the objective is not clear, we can not said if your code is right or not. However, this is what your code does. Suppose the first file is "firstFile.txt"; then this section:
SET newname=%%f
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"
is executed this way:
SET newname=firstFile.txt
SET newname=!newname:*.txt="c:\path to file that contains the listed names"!
Previous line replace from the beginning of newname until ".txt" (that is, the entire value) by "c:\path to file that contains the listed names", so the next line is executed this way:
move "firstFile.txt" ""c:\path to file that contains the listed names""
that correctly should move the file into the given path even if it contains a pair of quotes at each side.
If the objective would be "Rename files in a folder to the names listed in a text file one-by-one", then you must do a merge between two lists: the file list created by dir /b *.txt and the name list stored in the file.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\path to file that contains the listed names"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
If this is not what you want, please clearly describe the desired process...

Related

Automatically Create Folders Based on String in Filename

I need a batch file to create a process for a large list (2k) of TIF files in a local folder directory.
The filename structure is, for example: 12345_1.tif.
I need the batch to:
1 - Create a folder name based on the number(s) after the underscore, as this is the only constant in the naming. The folders only based on this sole number.
2 - Copy and move the file into the newly created folder.
In the example above, the batch would create a folder called 1 and then move the file 12345_1.tif into that folder. If it found another file such as 54321_1.tif, that file would also be moved to the "1" folder. In my files, the numbers after the _ range from 1 through 77, and there could multiple files that share the same number after the _.
I've observed some similar scripts online, but I need help to modify my requirement. Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
It seems you didn't understood my comment, so I post it here as an answer with complete code:
#echo off
for /F "tokens=1,2 delims=_." %%a in ('dir /B *.tif') do (
md "%%b" 2>NUL
move "%%a_%%b.tif" "%%b"
)

Find, Copy and Rename in BATCH

Good Morning!
Coming from another article, which i tried to adopt, but failed, i would like to ask my question in a separate post. I need to achieve two things:
Copy all files that contain a certain string in their filename from one directory to another, but only if that file does not already exists in that target directory. The filename could be something like EventLog_12345.txt and i would want to copy only the files where the filename contains EventLog.
In a set of files identify in every .txt file a certain string. This string indicates the line that contains the string i am looking for. I want to get to the end of this line and save the .txt file as a new .txt file with a new name based on the string i find at the end of this line. Example: My file is EventLog_12345.txt and somewhere in this file there is a line like this:
2018-06-22 08:21:19 0133 LET vVariable = 'h**ps://somedomain.com/test/1/2/4/jobs/joblog.XML'
The string indicating the line is vVariable.
The string i want to use within the new filename in this example is joblog.xml. The file should be stored as a new .txt file with the name: joblog_12345.txt. Note, that the length of the line can vary; so can the length of the domain string; also the names of the XMLs are different. The constant is that i always
want to have the name of the XML file which is always the last piece of the domain.
Adding info on efforts so far
Copy & Paste - this is actually working, but does not check whether a file already exists:
#echo off
for /f "delims=" %%a in (
'xcopy /l /e /y "\\myPath\*EventLog*.txt" "D:\Target\" ^|find "EventLog"'
) do copy "%%a" "D:\Target\"
For the identification of string and then SaveAs i dont really have anything. I was basically hoping i could somehow adjust the solution provided here: (Rename text files based on multiple strings in their contents)
For 1st
#echo off
Set "Target=D:\Target\"
for /f "delims=" %%A in (
'xcopy /l /e /y "\\myPath\*EventLog*.txt" "%Target%" '
) do if not exist "%Target%%%~nxA" copy "%%A" "%Target%"
For 2nd
process a list of files containing vVariable with findstr /I /M "vVariable" *_*.txt in %%A
Split the found name at the underscore %%B
search file to get the Line and exchange all / in the line to spaces to then
get the very last element in %%D
use %%~nD without extension to form the new name
:: Q:\Test\2018\05\22\SO_50982243.cmd
#Echo off & Setlocal EnableDelayedExpansion
For /f "delims=" %%A in ('findstr /I /M "vVariable" *_*.txt ') Do (
For /f "tokens=2delims=_" %%B in ("%%~nA") Do (
For /f "delims=" %%C in ('findstr /I "vVariable" ^<"%%A"') Do Set "Line=%%C"
Set "Line=!Line:/= !"
For %%D in (!Line!) Do Set "NewName=%%~nD_%%B%%~xA"
Echo Ren "%%~A" "!NewName!"
)
)
Sample output based on your information
> SO_50982243.cmd
Ren "EventLog_12345.txt" "joblog_12345.txt"
If the output looks OK remove the echo in front of ren to really rename.

batch script - rename copied file

Below batch script works great for copying the most recent file in a directory, but how can i rename the copied file accordingly?
pushd D:\sales\
for /f "tokens=*" %%a in ('dir /b ') do set newest=%%a
copy "%newest%" D:\test\
popd
The destination argument can be the directory to put the new file in, but you can also add the new filename to it. So instead of D:\test\ you could do something like D:\test\filename.ext
This improved code snippet renames your file to new_name.ext:
pushd D:\sales\
for /f "tokens=*" %%a in ('dir /b /o:-d /a:-d') do (
set "newest=%%~a"
goto :SKIP
)
:SKIP
copy "%newest%" "D:\test\new_name.ext"
popd
Improvements:
addition of new file name, of course...
defined sort order for dir to get the newest file first, and filtered out directories to get files only; the goto inside of for breaks the loop, so there is only one iteration; this might improve performance in case of a huge number of files;
inserted modifier ~ into for variable %%a to avoid any surrounding quotes; such are stated later at the destination in the copy command line;

I need to create a list of folders that contain a specific file type with a batch file

Using a batch file I'm trying to generate a list of only folders within a location that contain a certain file type, let's call it *.abc
at the moment I only know how to echo a DIR command output to a file called folder.lst, I would like to expand on that and try to either
a) echo only folders containing the *.abc file type to folder.lst
b) remove references in folder.lst of folders that do not contain the *.abc file type.
I also tried having a FOR loop check each line to see if a *.abc file existed in that location and skip it, if not, but I just could not get that to work, here is an example of what I had.
setlocal enableextensions enabledelayedexpansion
FOR /F "delims=" %%C in (folder.lst) do (
set temp=%%C
if not exist !temp!\*.abc (goto skip) else (goto resume)
:resume
then my actions live here
:skip
)
but I am aware I am doing something wrong here...I just do not know what.
Maybe the /R form of the for command will help:
for /r "basedir" %%a in (.) do if exist "%%~a\*.abc" (
echo %%a contains .abc file(s)
)
The %%a will be the directories you want (with a trailing \., but you should be able to not care or accommodate this).
There are problems with such of the script as you have posted in that you can'y use labels within a block statement. You've also not provided any examples.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "lastdir="
FOR /r "%sourcedir%" %%a IN (*.abc) DO IF "!lastdir!" neq "%%~dpa" (
SET "lastdir=%%~dpa"
ECHO %%~dpa
echo "!lastdir:~0,-1!"
)
GOTO :EOF
Each directory found will be echoed twice - one with the trailing \ and once without.
You would need to change the setting of sourcedir to suit your circumstances.
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%cd%"
for %%a in ("%root%") do for /f "delims=: tokens=2" %%b in ('
dir /a-d /s "%root%\*.abc" ^| find "\"
') do echo(%%~da%%~pnxb
This executes a recursive dir command searching for the indicated file type under the starting point (change root variable to suit your needs). For each found folder we retrieve the folder from the dir header that precedes the file list (the lines that contain a backslash).
To separate the path from the rest of the information in the line, the colon is used as delimiter. As this will leave the drive out of the retrieved information, an aditional for is used to retrieve the drive from the folder reference.
From the command line:
for /f "delims=" %a in ('dir /s /b *.abc') do echo %~dpa >> folders.lst
In a batch file:
for /f "delims=" %%a in ('dir /s /b *.abc') do echo %%~dpa >> folders.lst
The above commands will place only the folder names containing the *.abc files in folders.lst.
Notes:
% should be replaced by %% when the command is used in a batch file.
The ~dp part of %~dpa expands %a to a drive letter and path only. Remove the d if you don't want the drive letter. The p path includes a trailing \ which may be interpreted as an escape character by some commands.
The above commands start the search in the current directory. To search from the root of the current drive you can do cd \ first.
For more information see FOR /F Loop command: against the results of another command and Parameters.

Prefix file created datetime for multiple files with specific extension in a directory

I want to rename all the sql files in a directory by prefixing them with the date and time the file was created.
I am new to command prompt, came up with below code from what I read but it is not working.
for /f "delims=*" %%I in ('dir *.sql /b /s') do (
set dt=%%~tI
for /f "delims=/: " %%a in ('%dt%') do (set mydate=%%a)
for /f "delims=/: " %%b in ('%dt%') do (set mytime=%%b)
ren %%~nI.sql %mydate%%mytime%_%%~nI.sql)
Something like this should work if the batch file is in the same directory as the sql files:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=*" %%f in ('dir /b *.sql') do (
set createtime=%%~tf
set createtime=!createtime:/=!
set createtime=!createtime::=!
rename "%%f" "!createtime! %%f"
)
The 2nd and 3rd set createtime lines are replacing / and : (respectively) with null because those are invalid characters for a filename. If you prefer you could replace them with something else instead (e.g. -) by inserting this value to the right of the = character.
Note that the %%~t modifier used here (and in your example) actually gets the modified time of the file, not the created time. This would only be noticeable if your sql files are modified after they're created.
Also, there is nothing here to prevent it from prefixing the same files with additional timestamps if the script is executed more than once, so keep that in mind.
ren "%file" "%date:~0,2%.%date:~3,2%.%date:~6,4%-%time-somename.sql"

Resources