I have a series of files that I download/process regularly and need to use a batch file to rename. Each filename is something like word-word-word-datetime.csv. There is always a '-' between words and always -datetime before the '.csv' file extension. I need to remove the -datetime so that the files are named word-word-word.csv. In some cases there might be just one word before the -datetime but there can be a string of many words as well. I download these files and move them to a specific folder for processing, and there is already a batch file in the folder that I need to modify to also rename the files.
For example, I need the filenames below:
this-is-a-file-20200804134809.csv
another-file-20200804134750.csv
some-other-file-20200804134699.csv
file-20200804134389.csv
To be renamed to:
this-is-a-file.csv
another-file.csv
some-other-file.csv
file.csv
This answer is almost exactly what I need, but I'm not familiar enough with the syntax to modify it for renaming files with multiple hyphenated words (code from linked answer copied below).
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)
I was able to rename all of the .csv files in my folder, truncating the name to remove the last 15 characters as suggested by #Compo.
#echo off
setlocal enabledelayedexpansion
for %%f in (*.csv) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-15!%%~xf"
)
Here's a more robust example, using the advice I provided in the comments:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "EOL=| Delims=" %%G In ('Dir /B /A-D *-??????????????.csv ^
^|"%__AppDir__%findstr.exe" /IR ^
"\-19[0123456789]*\.csv$ \-20[0123456789]*\.csv$"') Do (
Set "BaseName=%%~nG"
SetLocal EnableDelayedExpansion
Ren "%%G" "!BaseName:~,-15!%%~xG"
EndLocal
)
Related
I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
FOR /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.
I have a bunch of Pdf files with names like so:
Malcolm Gaskill - History.pdf
Manfred B. Steger - Globalization; A Very Short Introduction.pdf
I want to rename them to get rid of everything before the first hyphen so they end up like:
History.pdf
Globalization; A Very Short Introduction.pdf
How do I go about doing this?
Thanks
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*-*.pdf" '
) DO (
FOR /f "tokens=1*delims=-" %%b IN ("%%a") DO (
FOR /f "tokens=*" %%d IN ("%%c") DO (
ECHO(REN "%sourcedir%\%%~a" "%%~d"
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Not particularly easy for a beginner, since the filenames may contain ; which is a separator like Space
First, perform a dir /b to get all of the required filenames which are applied to %%a using for /f with no delimiters.
Next, tokenise using - into %%b and %%c, then remove leading separators from %%c using tokens=* applying required filename to %%d.
Then mix-and-match to build the required rename command.
I have a Directory with a deep Directory->Sub-directory tree structure. I need to write a batch file to copy all the numbered files (files with names as digits and no alphabetic characters) from all the sub-directories.
For example, a sub-directory might contain the following files:
WR10091.txt
AX10091.htm
10091.txt
AX10091.xml
10091.xml
I need to copy 10091.txt and 10091.xml to another location. I can copy files like AX10091.xml and AX10091.htm by specifying AX*.*. But I cannot figure out how to copy just numbered files with no alphabetic characters. There are thousands of directories and the directory structure does not have any pattern (the depth of a tree branch can vary considerably).
Any help will be appreciated.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=%cd%"
set "target=x:\target\folder"
for /r "%source%" %%a in (*) do (
(for /f "delims=0123456789" %%b in ("%%~na") do (
break
)) || echo copy "%%~fa" "%target%"
)
In this code the for %%a will iterate over all the files under the indicated folder. For each of them, the for /f %%b will try to tokenize the file name (%%~na) using numbers as delimiters. If the file name only contains numbers, there will be nothing to process (only delimiters) and the inner for raises errorlevel. This is checked with conditional execution (the code after the || is executed if the previous command fails) and if errorlevel was raised the copy operation is echoed to console.
If the output is correct, remove the echo to perform the copy.
note: the break in the inner for loop is included just to have a command that does nothing when files with non numeric names are found.
#echo off
for /f "tokens=* delims=" %%a in ('dir /b /s /a:-d "*"') do (
echo %%~na|findstr /e /b /r "[1-9]*" >nul 2>nul && (
copy %%~fa c:\somewhere\
)
)
should be executed in the same directory as the files.
for /f "delims=" %%a in ('dir /b/s/a-d ^| findstr /reic:"\\[0-9][0-9]*\..*" /c:"\\[0-9][0-9]*"') do copy "%%~a" "targetDir"
This might not work with XP and/or Vista, but this can be fixed if needed (see What are the undocumented features and limitations of the Windows FINDSTR command).
I'd like to use Power Shell or a batch file to rename several files in a folder based on a list in a text file. Essentially I want to append the file names with the author's last names (which I have stored in a separate text file).
E.g. Currently I have:
C:\myfiles
9-ART-2013.pdf
4-EGO-2013.pdf
2-ART-2013.pdf
My text file (in same order as files):
C:\myfiles
_Smith
_Jenkins
_McMaster
I want the files to be renamed as follows:
9-ART-2013_Smith.pdf
4-EGO-2013_Jenkins.pdf
2-ART-2013_McMaster.pdf
I've seen similar problems where people want to recursively rename files but they are always using a generic common appending element like adding an underscore or pre-pending with folder name, etc.
e.g. https://serverfault.com/questions/6268/easy-way-to-rename-all-files-in-a-directory-in-windows
In PowerShell it would be:
$names = Get-Content c\myfiles
Get-ChildItem C:\somedir\*.pdf | Sort -desc |
Foreach {$i=0} {Rename-Item $_ ($_.basename + $names[$i++] + $_.extension) -WhatIf}
If it looks like it will copy correctly, remove the -WhatIf.
Any of the above are likely workable solutions with just a little tweaking but the simplest thing to do was to simply create one text file with each row containing "the current filename"... a TAB... then "the filename I wanted".
9-ART-2013.pdf 9-ART-2013_Smith.pdf
4-EGO-2013.pdf 4-EGO-2013_Jenkins.pdf
2-ART-2013.pdf 2-ART-2013_McMaster.pdf
Then save the file as rename_list.txt and create a batch file with the following code.
for /F "tokens=1,2" %%a in (rename_list.txt) do ren "%%a" "%%b"
pause
You can delete the pause line once you get it tweaked and running correctly. Just copy the rename_list.txt and batch files to the folder with the files you want to rename and run the batch file. If you have a large folder with many files names you can get them in a text file by running a batch file with the following line.
dir /D > filename_scrapper_output.txt
It will create a text file filename_scrapper_output.txt that you can use to start your rename_list.txt file for above.
Another way to achieve the same thing:
#echo off
setlocal EnableDelayedExpansion
rem Load the list of authors:
set i=0
for /F %%a in (myfiles.txt) do (
set /A i+=1
set "author[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F %%a in ('dir /b *.pdf') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "%%~Na!author[%%i]!%%~Xa"
)
EDIT: New method added
If the list of names file have the "OLD-Name NEW-Name" structure, then the code is much simpler:
for /F "tokens=1,2" %%a in (myfiles.txt) do ren "%%a" "%%b"
Note that the names must be separated by a space.
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<Text.txt (
for /f "tokens=1*delims=_" %%a in ('dir /b /a-d /o-d *-*-*.pdf') do if "%%b"=="" (
set "xand="
set /p "xand="
echo ren "%%~a" "%%~na!xand!%%~xa"
))
Taking Endoro's and simplifying:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<texts.txt (
for /f "tokens=* delims=* " %%a in ('dir /b /a-d /o-d *-*-*.pdf') do (
set "xand="
set /p "xand="
ren "%%~a" "%%~na!xand!%%~xa"
))
I have a series of files that have names like this:
CHART_LOAN_6516_20130502.PDF
CHART_LOAN_2158_20130502.PDF
CHART_LOAN_78986_20130502.PDF
Each file always starts with CHART_LOAN_ but the next number is different and the last number is the date it was created.
I would like to insert an 0_ after CHART_LOAN_number_ for each file. As listed below:
CHART_LOAN_6516_0_20130502.PDF
CHART_LOAN_2158_0_20130502.PDF
CHART_LOAN_78986_0_20130502.PDF
Through my research I've found out to insert the char but not when the name is changing with each file.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=CHART_LOAN_
SET new=CHART_LOAN_0_
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
SET newname=%%f
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"
)
The above code will change the static portion of the file to something I want, but I don't know how to modify the code to compensate for the changing loan number.
try this:
#echo off &setlocal
for /f "tokens=1-3*delims=_" %%i in ('dir /b /a-d *.pdf ^| find /i /v "_0_"') do ren "%%i_%%j_%%k_%%l" "%%i_%%j_%%k_0_%%l"
for /f "tokens=*" %%f in ('dir /b /a-d %old%*.pdf^|find /i /v "%new%"') do (
should see you right – but I'd add an ECHO temporarily before the MOVE to check it's working properly.
Interesting that you'd use *.jpg in your original though. Rather unlikely to work, I'd guess.
The renamed files will match the pattern ..._0_... but the originals won't – the closest they'll come is with CHART_LOAN_0.pdf. Hence, you find all the filenames that don't match the new mask (/v) case-insensitive (/i). The /a-d guards against the remote possibility of a directory name that matches the old mask. The caret before the pipe tells CMD that the pipe is part of the command to be executed by the for.