Ok so I'm super close to doing what I need to do.
I'm having an issue with my rename command and a double letter at the end of the folder. The folder names in the code have been changed for privacy, Spaces have been kept to show how the folders would be named.
The double letter is uppercase I (eye), this can't be changed.
Yes this file exists.
Example:
FolderII - error: The system cannot find the path specified.
Folder - Works
FolderI - works
for /r "C:\Folder Name" %%a in (*) do if "%%~nxa"=="FileFound" set p=%%~dpnxa
for /f "usebackq tokens=1* delims=." %%A in ("%p%") do set Build=%%B
for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G
for /f "tokens=3 delims=\" %%Z in ("%p%") do set filepath=%%Z
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set dbname=Logdb%year%.%Build%
REN "C:\Folder Name\%filepath%\%dbname%" "Logdb%month%-%day%-%year%.%Build%"
OUTPUT
EDITED!
CMD>REN "C:\Folder Name\FolderII\Logdb2020.ext" "Logdb11-23-2020.ext"
The system cannot find the file specified.
Added
CMD>REN "C:\Folder Name\Folder\Logdb2020.ext" "Logdb11-23-2020.ext"
THIS works
EDIT FOR CLARIFICATION*
I'll explain this how I intended it to work, which it does as long as the folder it's being assigned to doesn't have a II in it.
1st line: Search this particular folder for a file called "SYSCON" no extension, once found assign to p the file path of the file for 2nd line
2nd line:Open file found at 1st line and get the extension of the file listed inside the file and assign it to Build
3rd line:Get the current date to assign to the new file name in REN
4th line:Use the file path found in line 1 to get the folder name for the REN
5-7 set date variables
Line 8:Assign the new file name to variable
Line 9:Rename the old file at the location found to the new file name generated
I'm not a batch developer, I've literally written these lines as they work for me, but I'm always willing to learn how to do better, I'm a PHP programmer. This is a different project.
The folder structure is fluid for the application. The reason for the search for the initial file is to find the file in 1 of 4 folders and then get that actual folder name.
I can echo all the variables and see the correct file path, the correct file name and the correct new file name.
When it comes to rename the file in the folder with II, it fails to find the actual file to do the rename on, that's where I'm stuck.
IMAGE of Output echoed as it steps through the lines, for privacy sake I have to change the file names. Here's the CMD output for, I hope, better understanding
I'm not positive, based upon your lack of specific information, but as a best guess, I'd assume that something like this should perform the task, I think your example is trying to achieve.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Tokens=1-3 Delims=/ " %%G In (
'""%__AppDir__%Robocopy.exe" \: . /NJH /L | "%__AppDir__%find.exe" " 123""'
) Do Set "YYYY=%%G" & Set "MM=%%H" & Set "DD=%%I"
For /D %%G In (C:\Folder Name\*) Do For %%H In ("%%G\SYSCON"
) Do If "%%~aH" Lss "d" If "%%~aH" GEq "-" (
For /F "UseBackQ Tokens=1,* Delims=." %%I In ("%%H") Do Set "Build=%%J"
SetLocal EnableDelayedExpansion
Ren "%%G\Logdb%YYYY%.!Build!" "Logdb%MM%-%DD%-%YYYY%.!Build!"
EndLocal)
The example above expects the the string you're using for the Build variable is on the last non empty line of the target file, (ASCII text with CRLF line endings). If it is the only non empty line in that target file, then perhaps the following would be more useful:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Tokens=1-3 Delims=/ " %%G In (
'""%__AppDir__%Robocopy.exe" \: . /NJH /L | "%__AppDir__%find.exe" " 123""'
) Do Set "YYYY=%%G" & Set "MM=%%H" & Set "DD=%%I"
For /D %%G In (C:\Folder Name\*) Do For %%H In ("%%G\SYSCON"
) Do If "%%~aH" Lss "d" If "%%~aH" GEq "-" (
For /F "UseBackQ Tokens=1,* Delims=." %%I In ("%%H"
) Do Ren "%%G\Logdb%YYYY%.%%J" "Logdb%MM%-%DD%-%YYYY%.%%J")
It would seem that the data assigned to build contains trailing spaces and perhaps some invisible characters. The easy way would be to simply change 1* to 1,2.
Since Space is a default delimiter, %%B will be assigned the value between the first and second spaces on the line. Tough if you want spaces in the extension, but do you really want to use extensions with spaces?
The syntax SET "var=value" (where value may be empty; in which case var becomes undefined) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
Related
Using windows batch file, I am trying to echo a list of file extensions in a folder.
That will then be set as %var% and output to .txt file
#setlocal & #(for %%I in (*.*) do #set /a ext[%%~xI] += 1) & set ext[
from this post Windows command to get list of file extensions
My end game is I would like it to output like so
folder contents:
sample1.jpg
sample2.jpg
sample3.jpg
sample4.mp4
sample5.mp4
sample6.png
sample7.zip
sample8.zip
result:
.jpg, .mp4, .png, .zip
Any help is greatly appreciated, I hope I explained it clear enough
You already have defined a variable for each extension. Just use another for /f loop to concatenate the relevant part (the actual extension) of them:
#echo off
setlocal enabledelayedexpansion
for %%I in (*) do #set /a ext[%%~xI] += 1
for /f "tokens=2 delims=[]" %%a in ('set ext[') do set "var=%%a, !var!"
echo %var:~0,-2%
#setlocal ENABLEDELAYEDEXPANSION&#(for %%I in (*.*) do #set /a ext[%%~xI] += 1&IF DEFINED ext[all] (IF "!ext[all]!" equ "!ext[all]: %%~xI=!" SET "ext[all]=!ext[all]!, %%~xI") ELSE (SET "ext[all]= %%~xI"))&SET "ext[all]=!ext[all]:~1!"&set ext[
Why you want this all on one line, I've no idea.
Since you need to access the changed value of a variable within a loop, you need delayedexpansion. Note that Space.ext is appended each time a new extension is found, so there will be an extra space at the start of the list variable - hence the requirement to remove the first character before displaying the result.
I have a bat file that extracts the target of the given shortcut in the parameter
#echo off
set "paf=%*"
for /f "delims=" %%a in ('wmic path win32_shortcutfile where "name='%paf:\=\\%'" get target /value') do (
for /f "tokens=2 delims==" %%b in ("%%~a") do CALL SET shortcutPath=%%a
)
The return is like: C:\Users\MyUser\Desktop\SomeFolder\target.exe
All I want is to get the folder path without the executable name. This is a bat that I'll use constantly with different given shortcuts from many locations so it has to be relative.
On how to achieve this, I thought on exclude all characters from the last delimiter found, the delimiter should be \ and it will exclude anything on the right side of the last one found. The thing is I have no idea how to do that, or even if this is possible
Can someone bring some help on this?
Thank you
The simplest way is to use the already existing output Target name, instead of assigning the name shortcutPath to it. As long as your passed argument is a valid and working shortcut file, your resulting variable will be accessible as %Target%.
Set "Target=" & For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
Path Win32_ShortcutFile Where "Name='%Lnk:\=\\%'" Get Target /Value 2^>NUL'
) Do For /F "Delims=" %%H In ("%%G") Do Set "%%H"
Then given your Target variable contains the full path and file name, you can use variable expansion modifiers to return its drive and path only:
If Defined Target For %%G In ("%Target%") Do Echo %%~dpG
If you want it without the trailing backwards slash, you could return that by including another for loop, and changing the modifier:
For %%G In ("%Target%") Do For %%H In ("%%~dpG.") Do Echo %%~fH
Instead of CALL SET shortcutPath=%%a, use
SET "shortcutPath=%%~dpb"
The result should be C:\Users\MyUser\Desktop\SomeFolder\ in your given example.
For explanation, read and follow for /?:
%~dpI - expands %I to a drive letter and path only
Read more about using quotes in https://ss64.com/nt/set.html.
I am trying to get the file version of all the files inside a folder which I managed to do (but not in a good way) but now I want to stick the folder name along with the version so I would know which version is for which folder.
I am not very good in command line and only use it for some small tasks whenever I need it so my apology in advance..
Here is what I have done:
For /d %%a in (C:\high\low\*) Do (For /d %%* in (%%a) Do wmic datafile where name="%%~da\\high\\low\\%%~nx*\\bin\\Services.dll" get Version /value)
and I get output as:
`Version=2.2.0.1 Version=2.2.0.4 Version=2.2.0.4....Version=2.2.0.4
there are 20 folders under C:\high\low and I want to go into the bin directory of each sub folder so I can see which folder has been upgraded and which one is not.
Edit
There are more than 20 folders and structure is like this:
C:\high\low\office.Services.Bolton\bin\Services.dll
C:\high\low\office.Services.Slough\bin\Services.dll
C:\high\low\office.Services.Hull\bin\Services.dll
.
.
.
C:\high\low\office.Services.Cosham\bin\Services.dll
I want to check the version number of Services.dll and need the output as:
Bolton - 2.2.0.1
Slough - 2.3.0.1
Hull - 2.5.0.1
.
.
.
Cosham - 2.0.0.0
Thanks in advance..
Instead of stacking for /d you could do a dir /b/s to find all Services.dll and parse the nasty (cr,cr,lf) output of wmic with a for /f:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=*" %%A in (
'Dir /B/S C:\high\low\Services.dll ^|findstr /i "bin\\Services.dll$"'
) Do (
Set "DLL=%%~fA"
Set "DLL=!DLL:\=\\!"
For /F "tokens=1* delims==" %%B in (
'wmic datafile where name^="!DLL!" get Version /value^|findstr Version'
) Do For /f "delims=" %%D in ("%%C") Do Echo Version: %%D %%~fA
)
The first For /f parses the output of the commamd inside the '' iterating through each line passed in %%A (I prefer upper case variables to better distinguish between lower case~ modifiers.
Since Dir will allow wildcards only in the last element I can't do a Dir /B/S C:\high\low\*\bin\Services.dll
To ashure I get only Services.dll in a bin folder I pipe dir output to findstr /i "bin\\Services.dll$ (findstr uses by default a limited RegEx so the \ has to be escaped with another one, the $ anchors the expression at the end of the line).
The wmic command needs the backslashes in the path also escaped what is possible with string substitution (works only with normal variables)
In a (code block) we need delayed expansion to get actual values for variables changed in the code block, so ! instead of % for framing the variable names
The 2nd For /f parses wmic output splitting at the equal sign, assigning content to var %%C
EDIT added another for /f to remove the wmic cr
I am trying to find the following version number in a app.config file.
The file is XML format.
Line 8 in the file (Adding line in again as the greater/less than symbols were stripped from the post initially)
add key="ReleaseVersion" value="5.2.0.2"
I been using various FOR /F commands, have been close a couple of times.
However I have not been able to extract the 5.2.0.2 value and use as a variable
so far in my script.
Additionally while I am looking for this value 5.2.0.2, going forward the version number will change so I am not looking for a exact match e.g. "5.2.0.2", I am looking to capture what is in the inverted commas e.g. value="", and then using this as a variable in my script.
Example of what I have tried so far...
FOR /f "tokens=3 delims=5." %%a IN ('TYPE appsettings.config ^| FIND "ReleaseVersion"') DO SET do set word3=%%a
FOR /F delims^=^"^ tokens^=2 %%G IN ('FINDSTR /L "ReleaseVersion" "appsettings.config"')
FOR /f "tokens=3 usebackq delims== " %%G in (`appsettings.config`) do #echo %~G
Have tried a number of techniques but as yet, nothing has been successful.
Can post more information as required however that essentially covers the issue.
Supposing the add key="ReleaseVersion" value="5.2.0.2" portion is in a single line and the related value parameter appears after the ReleaseVersion substring, the following could work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem define constants:
set "SEARCH=ReleaseVersion"
set "KEYWORD=value"
rem get line of interest and assign it to `LINE`:
for /F "delims=" %%L in ('findstr /L "%SEARCH%" "app.config"') do (
set "LINE=%%L"
)
setlocal EnableDelayedExpansion
rem cut everything off up to the search string:
set "LINE=!LINE:*%SEARCH%=!"
rem cut everything off up to the keyword:
set "LINE=!LINE:*%KEYWORD%=!"
rem extract the version number:
for /F tokens^=1^ delims^=^"^=^/^<^>^ %%N in ("!LINE!") do (
set "VNUM=%%N"
)
rem transfer the version number over the `setlocal`/`endlocal` barrier:
endlocal & endlocal & set "VNUM=%VNUM%"
echo ReleaseVersion: %VNUM%
exit /B
The string portion of interest does not need to look exactly like shown above, but may contain more or less spaces (for example add key = "ReleaseVersion" value = "5.2.0.2"), or include the " or not (like add key=ReleaseVersion value=5.2.0.2). The only condition is that the attribute key needs to appear before the attribute value.
If the search line is precisely this one:
add key="ReleaseVersion" value="5.2.0.2"
... then this code should work:
#echo off
setlocal
for /F "tokens=3" %%a in ('findstr "ReleaseVersion" "appsettings.config"') do set %%a
set "value=%value:~1,-1%"
echo %value%
If the layout of the search line change (more blank spaces or other characters, less quotes, etc) then previous code should need an adjustment.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q34445384.txt"
FOR /f "tokens=3delims==" %%a IN (
'findstr /L /c:"add key=\"ReleaseVersion\" value=" "%filename1%"') DO SET "release=%%~a"
ECHO release=%release%
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q34445384.txt containing your data for my testing.
Simply find the target string using findstr and set the environment variable to the third token using delimiters of =, removing the quotes from the value with ~.
This assumes uniqueness of the target text and that the structure of the line is exactly as posted.
Assuming app.config is valid, well-formed XML, the best way to scrape the release version is to query it via XPath. You can invoke PowerShell for this.
#echo off
setlocal
set "psCommand=powershell "^
select-xml \"//add[#key^='ReleaseVersion']\" app.config ^| %%{ $_.node.value };^
""
for /f %%I in ('%psCommand%') do set "version=%%~I"
echo %version%
This will parse app.config for a node named "add" which has an attribute named "key" whose value is "ReleaseVersion", then will return that node's "value" attribute's value. for /f captures it to a batch variable.
Happy Friday Think-Tank!
I need some assistance with a Batch .BAT script. Specifically I need help with some "IF statement syntax"
I have a script that is renaming files. There are two files, one ending in four digits and the other ending in five digits. The files will be renamed with variables I have already pre-set earlier within my script.
So here is a scenario: We have two files in a directory located at
c:\Users\username\Desktop\test-dir
There are two files within test-dir:
file1.12345
file2.1234
A four digit ending is one variable type (VAR1), whereas a file ending in five digits is another variable type (VAR2).
I need an if statement to:
a) read all the files(s) with the chosen directory (without using a wildcard if possible).
b) determine based on the number of digits after the "." which variable to use.
c) once making that determination rename the file with the appropriate variables.
The final re-naming convention is as so: yyyymmddtype.1234/12345
So basically it would use the datestamp variable I already created, the type variable I already created to be injected by the if statement, and append with the original ending digits of the file.
I know this seems like a lot, but I am more so a bash script guy. I have all the elements in place, I just need the if statement and what feels like a for loop of some kind to tie it all together.
Any help would be great!
Thank you!
Sorry, not the option you where asking for. Instead of iterating over the full list checking each file for extension conformance, iterate over a list of patterns that will filter file list, renaming matching files with the asociated "type"
for %%v will iterate over variable list, for %%a will split the content of the variable in pattern and type, for %%f will generate the file list, filter with findstr using the retrieved pattern and rename matching files with the corresponding "type"
Rename command is preceded with a echo to output commands to console. If the output is correct, remove the echo to rename the files.
#echo off
rem Variables defined elsewhere
set "folder=c:\somewhere"
set "timestamp=yyyymmdd"
rem Rename pattern variables in the form pattern;type
set "var1=\.....$;type1"
set "var2=\......$;type2"
set "var1=\.[^.][^.][^.][^.]$;type1"
set "var2=\.[^.][^.][^.][^.][^.]$;type2"
setlocal enableextensions disabledelayedexpansion
for %%v in ("%var1%" "%var2%") do for /f "tokens=1,* delims=;" %%a in ("%%~v") do (
for /f "tokens=*" %%f in ('dir /a-d /b "%folder%" ^| findstr /r /c:"%%~a"') do (
echo ren "%folder%\%%~f" "%timestamp%%%~b%%~xf"
)
)
endlocal
#ECHO OFF &SETLOCAL
set "yyyymmdd=yyyymmdd"
set "VAR1=VAR1"
set "VAR2=VAR2"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\....."') do echo(ren "%%~a" "%yyyymmdd%%VAR1%%%~xa"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\......"') do echo(ren "%%~a" "%yyyymmdd%%VAR2%%%~xa"
remove echo( to get it working.
If I understand you then this will rename the two files using preset variables for each one:
for %%a in ("%userprofile%\Desktop\test-dir\*") do (
if "%%~xa"==".12345" ren "%%a" "%variableA%-%variableB%%%~xa"
) else (
ren "%%a" "%variableC%-%variableD%%%~xa"
)
)