I have some folders which says AA2017-123-TEXT and AA2017-124-TEXTS.
I'm trying to use batch to rename these file to AA2017-123 and AA2017-124 removing the text from the folder name. That is I want only the first 8 characters in a folder name.
I am using windows 7
To do exactly what have been asked for, namely splitting off a certain amount of characters, the following code snippet could be used (extracting the first 10 characters here for example):
for /F "delims=" %%D in ('dir /B /A:D "*"') do (
set "FOLDER=%%D"
setlocal EnableDelayedExpansion
move "!FOLDER!" "!FOLDER:~,10!"
endlocal
)
There are several ways to accomplish your task, IMO the simplest is using the hyphens as delimiters in a for loop.
In the cmd line
for /f "tokens=1,2* delims=-" %a in ('dir /B/ad *-*-*') do #echo ren "%a-%b-%c" "%a-%b"
In a batch
for /f "tokens=1,2* delims=-" %%a in ('dir /B/ad *-*-*') do echo ren "%%a-%%b-%%c" "%%a-%%b"
If the output looks ok remove the echo
Related
I have tried del "C:\path\?????.txt" to delete files having name of length 5 characters(abcde.txt etc.).
It does delete the files with 5 char names but it also delete files having less than 5 char name (abc.txt,abcd.txt etc.)
Any help will be appreciated.
The following complete batch-file, which uses where.exe, will only match, and subsequently try to delete, files whose name contains exactly five characters and the .txt extension. (all you need to do is to use a ? to represent each character).
#SetLocal
#Set "PATHEXT="
#For /F "EOL=? Delims=" %%G In ('""%__AppDir__%where.exe" "%UserProfile%\Desktop\New":"?????.txt" 2> NUL"') Do #Del /A/F "%%G"
#EndLocal
There are a few ways to do this, this is one of them:
#echo off & setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b "%userprofile%\Desktop\New\*.txt"') do (
set "str=%%~i"
if "!str:~5!" == "%%~xi" del "%%~i" /Q /S
)
I am trying to rename text files with a different text position.
Example :
20170811191008_marie.txt --> marie_txt_20170811191008
I have a very basic rename batch command to rename files in loop but I am trying to find out how I can change the position of the text within the filename.
Command:
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni txt%%~xi"
Per your wish
for /f "tokens=1* delims=_" %%i in (
'dir /b /a-d *_*.txt ^|findstr /i "^20[0-9]*_.*\.txt$"'
) do echo ren "%%i_%%j" "%%~nj_txt_%%i"
> SO_45673483.cmd
ren "20170811191008_marie.txt" "marie_txt_20170811191008"
EDIT forgot to mention to remove the echo in front of ren to really execute the rename once you are shure it does want you want.
For multiple file extensions, you could always use something like this:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "EOL=_ Tokens=1* Delims=_" %%A In ('Where .:??????????????_*.*') Do (
Set "fx=%%~xB"
Ren "%%A_%%B" "%%~nB_!fx:~1!_%%A" 2>Nul)
You can of course still specify .txt instead of .* but there would be no need to hard code _txt_ into the code following it.
I try to use two for loops to get a simple job done:
The first for-loop brings me a list of files,
the second for-loop then should joind me the content of all files together in a tempfile (and later ftp it somewhere but thats not the problem).
So this is my code so far:
setlocal enableDelayedExpansion
REM FILE-MERGER
if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
echo %%x
for /f tokens^=*^ delims^=^ eol^= %%f IN (%%x) DO echo %%f >> %temp%\ZS_aus_Files.csv
)
%%x so far has the complete path in it with spaces as well.
In the second for-loop (%%x) throws an error because of the space.
Output of my code is something like:
C:\Documents\accounting\file with spaces.csv
The file "C:\Documents\accounting\file" cannot be found.
Putting (%%x) in qoutes ("%%x") makes the output a filelist instead a merged content file.
I realy stuck here, hope someone can help me with that.
try with
if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
echo %%x
for /f usebackq^ tokens^=*^ delims^=^ eol^= %%f IN ("%%x") DO echo %%f >> %temp%\ZS_aus_Files.csv
)
This is not documented by MS at all in for help but when usebackq is used with double quotes it can be used for file names and processes file names with spaces.
For example, I have the folder d:\temp\ and four word document files in it (.doc)
I know that
dir /b "d:\temp"
will give me
File1.doc
File2.doc
File3.doc
File4.doc
But how can I do it so that there are only file names without extensions?
like
File1
File2
File3
File4
for %a in ("d:\temp\*") do #echo %~na
or for batch file:
for %%a in ("d:\temp\*") do #echo %%~na
to display also directories you can use:
for /f "delims=" %%a in (' dir /f "d:\temp\*"') do #echo %%~na
Another version with slight differences from the ones above:
for /f %x in ('dir /b /on *.doc') do #echo %~nx
The following code, when run via a batch file that uses #echo off, should work:
for /f "skip=7 tokens=5 delims=. " %%g in ('dir d:\temp') do echo %%g
Will print all file names without spaces in them. Tested and does so for my F:\ thumb drive.
Edit: for /f "tokens=1 delims=." %%g in ('dir /b d:\temp') do echo %%g
Will print out files and directories in a given path with spaces and is simpler anyway.
I can't find a specific mention of my issue, though it seems like it would be a common problem.
I'm trying to loop through all batch files in a directory, regardless of how deep.
Here is what I have:
for /f "tokens=* delims=" %%a in ('dir %DIR% /s /b *.bat') do (
if not exist %%a\* echo %%a
)
Where DIR is set beforehand. I'm echoing only files.
Clearly this is wrong as it outputs firstly all files, THEN all batch files.
Seems I need to somehow merge the *.bat specifier and the %DIR% variable but I'm not sure how to do this.
for /r %DIR% %%a in (*.bat) do ( ...
You question is somewhat vague to me, but I think this is what you want:
#echo off
setlocal
set DIR=h:\scripts
for /f "usebackq delims=;" %%q in (`dir %DIR%\*.bat /s/b/a-d`) do (
echo %%q
)
endlocal
This puts all .bat files into %%q.