I would like to loop through a folder and obtain only the last 13 characters of the file names. Part of what I did is as follows:
for /r . %%g in (*.pdf) do (
set var=%%~g
echo %var:~-13%
)
The problem I have is that it is only printing one file name for all the files in the folder. E.g I have the following file names as input :
ARC_1384343086954570285.pdf,
ARC_1384343288950263728.pdf,
ARC_1384343297950370887.pdf.
The output comes out as :
950370887.pdf
950370887.pdf
950370887.pdf
Any time you set a variable inside of a for loop, you have to use delayedexpansion to access it.
setlocal enabledelayedexpansion
for /r . %%g in (*.pdf) do (
set var=%%~g
echo !var:~-13!
)
Related
I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.
I have a batch file, which I will post below, I am trying to have it copy the first line in a file and then stores it in a variable.
#echo off
setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "delims=" %%a in ("MavenInstructions.txt") do (
set currentline=%%a
set filecontent=!filecontent!%SEPARATOR%!currentline!
)
echo The file contents are: %filecontent%
echo %filecontent%
pause
The first line of the MavenInstructions.txt is TEST LINE but what I get when I run my batch file is:
The file contents are: /MavenInstructions.txt
/MavenInstructions.txt
Press any key to continue . . .
for /f "usebackqdelims=" %%a in ("MavenInstructions.txt") do (
usebackq is required if the filename is "quoted".
btw - set filecontent=!filecontent!%SEPARATOR%%%a
would suffice.
I'm currently looping threw the subfolders of a known directly. I would like to grab the folder that contains the file I'm looking for, but only that folder's name, not the whole path, and preferably in a different variable to be use later on in the batch file ...
pause
CD c:\%username%\testFolder\Program\OLD\
For /R %%G in (test.t?t) do Echo %%G
pause
Now, this shows me the file I'm looking for : C:\User\testFolder\Program\OLD\myfile\exemple1\test.txt
what I would like is to replace the Echo %%G to set in a variable the "unknown" folders and subfolder, something along the line of
For /R %%G in (test.t?t) do set var1 = myfile\exemple1
anyone can point to what I'm missing ?
If you for /? in a cmd console, you'll see runtime variables on the last couple of pages. Using %%~dpG notation you can get the full drive\path specification of matched files. Then, using substring substitution and delayed expansion, replace %CD% with nothing. Finally, you can strip the leading and trailing backslash with a numeric substring -- !varname:~1,-1!.
#echo off
setlocal enabledelayedexpansion
cd /d "c:\%username%\testFolder\Program\OLD\"
for /R %%G in (test.t?t) do (
set "var1=%%~dpG"
set "var1=!var1:%CD%=!"
if "!var1!"=="\" (echo .\) else echo(!var1:~1,-1!
)
If you wish, you can prevent echoing duplicates by echoing only if the previous match does not equal the current one.
#echo off
setlocal enabledelayedexpansion
cd /d "c:\%username%\testFolder\Program\OLD\"
for /R %%G in (test.t?t) do (
set "var1=%%~dpG"
set "var1=!var1:%CD%=!"
if not "!var1!"=="!prev!" (
if "!var1!"=="\" (echo .\) else echo(!var1:~1,-1!
)
set "prev=!var1!"
)
I've got some *.Xml files in a directory and its sub-directories. I need to loop through the XML files which have a specific constant at the end of their file name, and then echo/print their names without the constant part nor the extension (.Xml).
For example: these are the file names I have:
FileAAA_Constant.Xml
FileBBB.Xml
FileCCC.Xml
FileDDD_Constant.Xml
And this is the output I need:
FileAAA
FileDDD
I've tried this command:
For /R %%X in (*_Constant.Xml) do echo %%~nX
Which outputs this:
FileAAA_Constant
FileDDD_Constant
As you can see, it has removed the extension only, while I need to remove "_Constant.Xml" as well.
This works if the file names contains only one underscore, as indicated in your example:
for /F "delims=_" %%X in ('dir /S /B *_Constant.Xml') do echo %%X
If the desired file names may contain more than one underscore, use Pokechu22's answer.
Fairly easy if the exact length of the phrase is known; you just need to use the %var:~0,-3% syntax. Since "_Constant" is 9 chars long, you would want %var:~0,-9%, which takes text from the start (0) to 9 chars from the end (-9). Aditionally, delayed variable expansion also must be enabled with setlocal ENABLEDELAYEDEXPANSION for this to be run inside of your For loop.
Here's a full example:
#Echo off
setlocal ENABLEDELAYEDEXPANSION
for /R %%X in (*_Constant.Xml) do (
set FileNameTemp=%%~nX
echo !FileNameTemp:~0,-9!
)
Note that if you have a file named just _Constant.xml, this will produce "ECHO is off." rather than "" (no output). This can be solved by changing echo !FileNameTemp:~0,-9! to echo. !FileNameTemp:~0,-9!, but that puts a space before each output.
Here's the loop alone:
for /R %%X in (*_Constant.Xml) do (
set FileNameTemp=%%~nX
echo !FileNameTemp:~0,-9!
)
What I want to accomplish is to rename all the .exe that I have in a folder.
Sample of random name should be "589uday5xpsa9iz.exe"
I would appreciate any help I can get on this, I have been trying to figure it out for a couple of days now.
Try this :
#echo off
setlocal enabledelayedexpansion
:://The string length of the output
set $Lcode=16
set $#=#
:://The char Map
set $l="#0=0" "#1=a" "#2=B" "#3=c" "#4=d" "#5=E" "#6=f" "#7=g" "#8=H" "#9=I" "#10=j" "#11=K" "#11=1" "#13=2" "#14=3" "#15=4" "#16=5"
:://Evaluating the char MAP
for %%a in (%$l%) do set %%~a
:://Looping in the directory for .exe files
for /f "delims=" %%x in ('dir /b/a-d "*.exe"') do (
for /l %%a in (0,1,%$Lcode%) do (
call:rand
)
ECHO ren "%%x" !$Fstring!.exe
set $Fstring=
)
pause
exit/b
:://Getting the random Char
:rand
set /a $n=!random!%%17
set $Fstring=!$Fstring!!#%$n%!
I made a char MAP of just 15 char but you can increase it with all the char (up and downcase + number). If you do it. You have to increase the value in !random!%%17
with the value of the total CHAR map(+1).
You can increase the length of the random string by changing the value in the variable : $Lcode (16) in this example.
The output :
ren CnpjSdee.exe 2aHg5I22EBBE2ff5.exe
ren DbatchCnpj.exe EIIg2E54aHHIEgfHE.exe
ren NTStreamColor.exe jg03f3dIfBfIfHj2.exe
ren savedialog.exe EgfdajIcdc2cf03E.exe
Press any key to continue. . .
If it's OK Remove the ECHO to realy rename the files.
this is a simple batch that will work from within the folder that contains the files to be renamed you could make a shortcut to use it from the desk top. use copy and paste for long names. C:\Documents and Settings\Owner\Desktop\New Folder\Rename.bat (file = Rename.bat) don't use the whole path.
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo File Renamed
ping -n 3 127.0.0.1 >NUL
goto begin
hope this works for you.
a much simpler approach ... try a for loop that cycles through all files in that folder and renames them one at a time giving each a unique number ...
#echo off
for /L %%n in (1 1 %random%) do (
for %%a in (*.exe) do (
rename "%%a" "%%a_%%n.exe"
)
)
so %%a represents all the files in that folder and %%n represents the unique number that is going to be assigned to each file in the for body separated with _ symbol
to use this batch file you have to place it in the folder containing your .exe files
this would rename files to original name_1 then original name _2 and so on if you want it to be completely random use only %random% in bracket of the first for loop and do away with the iteration "1" (see below) also at the renaming part do away with %%a to omit the original name of file so that you remain with only random numbers also see below e.g
#echo off
for /L %%n in (%random%) do (
for %%a in (*.exe) do (
rename "%%a" "%%n.exe"
)
)
hope this helps .....