I have a series of files that I need to rename on a daily basis. The files I receive have the following format: yyyyMMdd_hhmmss_xxx.someFileName.txt I need to strip out the time stamp in the middle as well as the three digit field preceeding the filename and leave the date and the "someFileName.txt" piece. The resulting filename should look like: yyyyMMddsomeFileName.txt
I'm pretty clueless when it comes to bat files, I've done some experimenting:
#setlocal EnableDelayedExpansion
#for %%i in (.\*.txt) do call rename %%i
:rename
#set dateString=%%i:~0,8%
#set nameString=%%i:~20%
#set combinedString=%dateString%%nameString%
#echo %combinedString%
Clearly, this doesn't actually rename anything yet. It's just supposed to print the combinedString output. I'm getting a syntax error: "The syntax of the command is incorrect ~0,8 ~20"
What's going on here? What's the correct approach for this?
This should work for what you wanted.
#echo off
for /f "delims=" %%X in ('dir /a:-d /b *.txt') do (
for /f "tokens=1,2,3,* delims=_." %%A in ("%%~nxX") do (
echo %%A%%D
)
)
Replace the echo command with ren "%%~fX" "%%A%%D" when you want to rename them.
Related
I'm trying to automate renaming of files based on a CSV such as the one shown below:
Name,FullName
John,JohnDoe
Jane,JaneDoe
Joe,JoeDoe
Let's say I have 3 text files within the same folder of my .bat called John.txt, Jane.txt, Joe.txt and I want to rename John.txt to JohnDoe.txt, etc.
I am getting "The system cannot find the file specified" no matter how much I alter the filepath in my rename. Here is a basic rundown of what I have. What am I doing wrong here or what other way should I approach this? I appreciate all feedback.
#echo off
setlocal enabledelayedexpansion
set csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (!csvpath!) do (
set person=%%g
set name=%%h
echo My name is !person! and my full name is !name!
rename !person!.txt !name!.txt
)
pause
This is how I would do it:
#echo off
set "csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv"
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (`findstr /v /c:":-:-:" "%csvpath%"`) do (
echo My name is "%%g" and my full name is "%%h"
rename "%~dp0\%%g.txt" "%%h.txt"
)
pause
This code is a bit cleaner and more robust, in that file paths and names can have special characters (like &) without breaking the script.
findstr /v /c:"SEARCHSTRING" "FILEPATH" tells findstr to print every line within FILEPATH excluding (/v) lines with SEARCHSTRING. This doesn't really change much from what you had previously, however it is a bit more robust.
In the rename command, I set it to %~dp0 and then the file name, %~dp0 is the path to where your .bat script is.
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
)
I am using batch script for windows command line on windows 7. I am currently trying to get the newest file from each sub directory and print them to the screen. For example, if I have:
C:\Home\Hi\Folder1\a 01/05/2016
C:\Home\Hi\Folder1\b 01/10/2016
C:\Home\Hi\Folder2\x 03/05/2016
C:\Homeh\Hi\Folder2\y 03/1/2016
It would return: folders b and x.
I have written some script to attempt this, but it only returns the newest file in the last directory which in my example would be x. The script is:
FOR /R "C:\Users\Maxx\Desktop\tools" %%G in (.) DO (
Pushd %%G
FOR /F %%I IN ('DIR %%G /B /O:D') DO SET NEWEST=%%I
ECHO %NEWEST%
Popd )
Echo "back home"
If anyone knows how to get the newest file from each subdirectory that would be great.
Note: I have looked at various other examples such as: Generate a list of the newest file in each subdirectory in windows batch, which has been very helpful in building what I have now, but ultimately it did not work.
You need to apply delayed expansion as you are writing and reading variable NEWEST within the same block of code:
setlocal EnableDelayedExpansion
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
echo !NEWEST!
)
popd
echo back home
endlocal
Alternatively, replace echo %NEWEST% by call echo %%NEWEST%%:
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
call echo %%NEWEST%%
)
popd
echo back home
In addition, I improved quoting.
#ECHO OFF
SETLOCAL
FOR /R "C:\106x" %%G in (.) DO (
Pushd "%%G"
SET "first="
FOR /F "delims=" %%I IN ('DIR . /a-d /B /O:-D 2^>nul') DO IF NOT DEFINED first SET first=Y&ECHO %%~dpI
Popd
)
Echo "back home"
GOTO :EOF
For each directory, go to the directory, clear a flag first, scan the directory for files (not directories), basic format, reverse-date order, suppressing any "file not found" reports from empty directories.
On finding the first (ie youngest) file set the flag first and report the filename - or parts of the filename you require.
Once first has been set, the report mechanism will not be invoked as if defined works on the current value of the variable.
Please note that when specifying dates, it's advisable to state the format that you are using - or at least use a date where the day number is >12, which should be adequate. For instance, your date "01/10/2016" may be interpreted as Jan 10th or Oct 1st, depending on the respondent's locale. "13/10/2016" or "10/13/2016" would make the issue obvious.
I have a folder where I have millions of file somewhat like
XXXXXX_156_1400093_20160507011119.psv
From this format I want a batch script which may ask me to input date in format yyymmdd seach for that date in the file name. Create a folder with the date and move all the files related to the specific date there.
I tried multiple examples and since I dont know scripting please help me.
Regards
Saurabh
I have tried the following code:
#ECHO on
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\sswarpal\Desktop\files"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=_" %%a IN (
'dir /b /a-d "*.psv"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
a batch script which may ask me to input date in format yyymmdd seach
for that date in the file name. Create a folder with the date and move
all the files related to the specific date there.
Keep it simple. Use wildcards with move instead of a complicated for loop
set /p "day=Enter Date (YYYYMMDD): "
md %day% 2>nul
move "*%day%??????.psv" %day%
If you want to do it automatic (like your code suggests): just using random parameters won't help (token 1 %%a is XXXXXX, but you want to create a subdir with the date (part of token 4)). Your parameters to move seems totally random.
Try this instead:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\sswarpal\Desktop\files"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=_" %%a IN ('dir /b /a-d "*.psv"') DO (
set day=%%d
set day=!day:~0,8!
echo md !day! 2>nul
echo MOVE "%%a_%%b_%%c_%%d" .\!day!\
)
popd
Remove the words echo when the output looks like what you want.
Save the following code in a batch file (e.g. myMover.bat). When running the file from a command line window, supply the date you want as the sole parameter on the command line, as in:
myMover 20160508
Here's the code:
#ECHO off
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
if "%1"=="" goto syntax
SET sourcedir="C:\Users\sswarpal\Desktop\files"
PUSHD %sourcedir%
if not exist %1 MD %1
FOR /F %%i IN ('dir /b *_%1*.psv') DO move "%%~ni%%~xi" %1
POPD
goto :eof
:syntax
Echo.
Echo Syntax:
Echo %0 YYYYMMDD
Echo.
Basically I'm trying to write a batch file to insert some code into multiple files. Here are the details of what I'm tring to accomplish:
1. The input string comes from a file test.txt.
2. The string needs to be inserted as the second line of destination files.
3. Destination files are all the .xml files under the same direction as the batch file.
I suppose I should use a FOR loop to go through all .xml files. Something like
for /f %%i in ('dir /b *.xml') do ()
I've read though some tutorials and posts but can't find a way to add anything to files in a loop. Using Echo or TYPE doesn't seems to work for each file in a loop. How do I modify files in a loop?
Also to insert to a certain number of line some post say the file needs to be put into a variable. But my files are pretty large, which I don't want to put into variables. Is there another way to insert into a certain line in a file?
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN ('dir /b *.xml') DO (
SET line2=Y
(
FOR /f "usebackqdelims=" %%x IN ("%%i") DO (
ECHO(%%x
IF DEFINED line2 TYPE Line2.txt
SET "line2="
)
)>"%%~ni.lmx"
)
GOTO :EOF
This should work for you - but it will delete empty lines.
#echo off
set /P string=< test.txt
for %%a in (*.xml) do (
(for /F "usebackq tokens=1* delims=:" %%b in ('findstr /N "^" "%%a"') do (
if %%b equ 2 echo %string%
set "line=%%c"
setlocal EnableDelayedExpansion
echo(!line!
endlocal
)) > "%%a.new"
)
New files have .xml.new extension; you may add a couple lines to delete original .xml files and rename .xml.new ones to .xml.