Rename file with different text position in batch - batch-file

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.

Related

Batch script to delete files in folder that does not contain certain word

I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.

batch: second for loop with spaces in dir name are not working

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.

Changing folder name with names from a file using batch

I want to write a batch script to rename folders in a directory.
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
So if I have 20 folders, 20 names would exist in file to rename each folder.
What I have so far:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %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!"
))
The above script didn't give me the desired result.
Not tested:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

Batch file to do everything in a directory

So I'm looking to make a jar to take everything in a directory of type "jar" then have it list it's directory name, and it's filename:
So something like this:
Listing of files in Directory c:\abc\123
myJar1.jar
myJar2.jar
...
myJar10.jar
How I want my output to appear is something like:
<jar href="123/myJar1.jar"/>
<jar href="123/myJar2.jar"/>
...
<jar href="123/myJar10.jar"/>
I've been having a lot of issues with this script, I can't seem to take variable that I'm setting and get them to append to text how I normally do.
Any help would be appreciated.
There is no need for an intermediate file, nor environment variables.
#echo off
>myJars.txt (for %%D in (.) do for %%F in (*.jar) do echo ^<jar href="%%~nD/%%F"/^>)
If you really must include hidden and system files, then the following longer version works
#echo off
>myJars.txt (for %%D in (.) do for /f "eol=: delims=" %%F in ('dir /b /a-d *.jar') do echo ^<jar href="%%~nD/%%F"/^>)
Both of the above can be reformatted to be a bit more readable:
:: excludes hidden and system files
#echo off
>myJars.txt (
for %%D in (.) do (
for %%F in (*.jar) do (echo ^<jar href="%%~nD/%%F"/^>)
)
)
:: includes hidden and system files
#echo off
>myJars.txt (
for %%D in (.) do (
for /f "eol=: delims=" %%F in (
'dir /b /a-d *.jar'
) do (echo ^<jar href="%%~nD/%%F"/^>)
)
)
Turns out that my batch-fu is way strong:
#echo off
dir /b /A *.jar > myJars.txt
for %%* in (.) do set CurrDirName=%%~n*
for /F "tokens=*" %%A in (myJars.txt) do echo ^<jar href^="%CurrDirName%/%%A" /^> >> myJars2.txt

Resources