I have a huge text file which look like this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
The desired output is this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
I have tried other relevant posts here and on other communities but could not exactly get what I want.
UPDATE
This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.
Here's a PowerShell answer in case you like PS.
Get-Content C:\Path\To\File.csv |
Where{$_ -match '^(.*,)([^,]*)$'} |
ForEach { "{0}MI-{1}" -f $Matches[1], $Matches[2].Padleft(2,'0') } |
Set-Content C:\Path\To\NewFile.csv
The next code does what you want except for filling with zero the last token when is less than 10, hope it helps.
EDIT: I figured out a way to insert a leading zero when the last number is less than 10. A little bit ugly but does it. :)
#echo off
setlocal EnableDelayedExpansion
for /F "delims=, tokens=1-8" %%A in (f.txt) do (
set /a "t=%%H-10"
if "!t:~0,1!" equ "-" (set "n=0%%H") else (set "n=%%H")
echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G,MI-!n!>>f.new.txt
)
move /Y f.new.txt f.txt >nul 2>&1
For file (f.txt in this case):
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
Produces the following result (also in f.txt): updated
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
Here is a cmd batch file that relies on a nice hack to split off the last item of a comma-separated list, independent on how many commas occur in the string. The basic technique is shown in the following; note that this requires delayed expansion to be enabled:
set "x=This,is,the,original,list."
set "y=" & set "z=%x:,=" & set "y=!y!,!z!" & set "z=%" & set "y=!y:~1!"
echo ORIGINAL: %x%
echo LAST ITEM: %z%
echo REMAINDER: %y%
So here is the code of the script, holding the above method in a sub-routine called :GET_LAST_ITEM:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (specify the CSV file by the first argument)
for /F "usebackq delims=" %%L in ("%_FILE%") do (
call :GET_LAST_ITEM LAST REST "%%L"
setlocal EnableDelayedExpansion
set "LAST=0!LAST!"
echo(!REST!,MI-!LAST:~-2!
endlocal
)
endlocal
exit /B
:GET_LAST_ITEM rtn_last rtn_without_last val_string
::This function splits off the last comma-separated item of a string.
::Note that exclamation marks must not occur within the given string.
::PARAMETERS:
:: rtn_last variable to receive the last item
:: rtn_without_last variable to receive the remaining string
:: val_string original string
setlocal EnableDelayedExpansion
set "STR=,%~3"
set "PRE=" & set "END=%STR:,=" & set "PRE=!PRE!,!END!" & set "END=%"
endlocal & set "%~1=%END%" & set "%~2=%PRE:~2%"
exit /B
This is the answer that #RomanPerekhrest provided at my cross-question (I was also seeking unix/perl solutions) here:
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8="MI-"sprintf("%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Currently, I have to upload a bunch of excel sheets to a network shared folder. Each of these files has the date they were created appended at the end of the filename. Then I have to remove the earlier duplicates leaving just the latest dated versions.
Basically it looks likes this...
Before:
apples 2019.07.01.xlsx
apples 2019.07.07.xlsx
oranges 2019.07.01.xlsx
bananas 2019.07.01.xlsx
After:
apples 2019.07.07.xlsx
oranges 2019.07.01.xlsx
bananas 2019.07.01.xlsx
I stumbled upon a possible solution, which was to create a batch-file to recursively go through the folder and do this. However, I am unsure where to start.
I read this other stackoverflow article, which is pretty close to what I want to do but I am having trouble adjusting it to my needs. Any assistance would be appreciated.
Edit2: this code worked for me:
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=PLOG - * - ????.??.?? - *.xlsx"
SET "_CurrentFile="
)
FOR /F "Tokens=1-2* Delims=-" %%A IN ('DIR /A-D /O-N /B "%_PathToCheck%\%_FileGlob%"') DO (
IF /I "!_CurrentFile!" EQU "%%A-%%B" (
ECHO.Deleting: "%_PathToCheck%\%%A-%%B-%%C"
DEL /F /Q "%_PathToCheck%\%%A-%%B-%%C"
) ELSE (
ECHO.
ECHO.New File Found: "%%A-%%B"
ECHO.-----------
ECHO.Retaining: "%_PathToCheck%\%%A-%%B-%%C"
SET "_CurrentFile=%%A-%%B"
)
)
You may use the same approach you would use if you do this job by hand: review the file list and every time that a file appear with the same name than the previous one, remove the previous one... Simple! Isn't it? ;)
#echo off
setlocal EnableDelayedExpansion
rem Initialize the "previous name"
set "lastName="
rem Process files in natural order, that is, the same order showed in the question
rem and set %%a to name and %%b to rest: date plus extension
for /F "tokens=1*" %%a in ('dir /B /A:-D /O:N *.xlsx') do (
rem If previous name is not the same as current one
if "!lastName!" neq "%%a" (
rem Just update previous name and date
set "lastName=%%a"
set "lastDate=%%b"
) else (
rem Remove the previous file
ECHO del "!lastName! !lastDate!"
rem and update the previous date
set "lastDate=%%b"
)
)
This solution assumes that the name and the date parts are separated by exactly one space...
EDIT: New method added, after several confusing changes made by the OP
#echo off
setlocal EnableDelayedExpansion
set "lastName="
for /F "delims=" %%a in ('dir /B /A:-D /O:N *.xlsx') do (
set "currName="
set "currFile="
for %%b in (%%~Na) do (
set "part=%%b"
set "currFile=!currFile! !part!"
if "!part:.=!" equ "!part!" set "currName=!currName! !part!"
)
if "!lastName!" neq "!currName!" (
set "lastName=!currName!"
set "lastFile=!currFile!"
) else (
ECHO del "!lastFile:~1!.xlsx"
set "lastFile=!currFile!"
)
)
Example of input files:
apples 2019.07.01.xlsx
apples 2019.07.07.xlsx
oranges 2019.07.01.xlsx
bananas 2019.07.01.xlsx
apples 2019.07.01 proof1.xlsx
apples 2019.07.07 proof1.xlsx
PLOG - Organic Valley - 2019.07.01 - (DAI) OG Cream Cheese.xlsx
PLOG - Organic Valley - 2019.07.07 - (DAI) OG Cream Cheese.xlsx
PLOG - Organic Valley - 2019.07.10 - (DAI) OG Cream Cheese.xlsx
Output:
del "apples 2019.07.01.xlsx"
del "apples 2019.07.01 proof1.xlsx"
del "PLOG - Organic Valley - 2019.07.01 - (DAI) OG Cream Cheese.xlsx"
del "PLOG - Organic Valley - 2019.07.07 - (DAI) OG Cream Cheese.xlsx"
Here is the newest version for you Greg.
Although originally I and a couple of others wrote versions which would work for date modified using a different logic (create variables for all of the files' names then sort them again, or do sets of compares) I realized we could still accomplish the goal in a single loop and without needing so many temp variables and without having to have more complex logic in that scenario and this one, so I took a few minutes and created that version.
Essentially we just need to define a variable with the File's name that has already been found, since we know they are ordered correctly date-wise, and only need to worry about removing the duplicate named files.
To do so we can use SEt or IF DEFINED, I prefer IF DEFINED here since I can use regular IF ( ) THEN ( ) ELSE ( ) logic as already defined in the script. (Note the items in Italic here are not terms that can be used in a CMD script, but I am writing them to clarify the normal logic of the IF construct)
We could use SET "[Variable Name]" instead, and test if success or failure using || or &&, but that would be more re-write and unnecessary here.
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=C:\T\DT"
SET "_FileGlob=PLOG - * - ????.??.?? - *.xlsx"
SET "_CurrentFile="
SET "_MatchList="
)
FOR /F "Tokens=1-3* Delims=-" %%A IN ('
DIR /A-D /O-N /B "%_PathToCheck%\%_FileGlob%"
') DO (
SET "_CurrentFile=%%A-%%B-%%D"
SET "_MatchList=!_CurrentFile: =_!"
IF DEFINED _MatchList_!_MatchList! (
ECHO.Deleting: "%_PathToCheck%\%%A-%%B-%%C-%%D"
DEL /F /Q "%_PathToCheck%\%%A-%%B-%%C-%%D"
) ELSE (
ECHO.
ECHO.New File Found: "!_MatchList!" Date-Stamp: %%C
ECHO.-----------
ECHO.Retaining: "%_PathToCheck%\%%A-%%B-%%C-%%D"
SET "_MatchList_!_MatchList!=%%A-%%B-%%D"
)
)
The previous version which conformed to the standards for only the left side of the file name being unique.
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=PLOG - * - ????.??.?? - *.xlsx"
SET "_CurrentFile="
)
FOR /F "Tokens=1-2* Delims=-" %%A IN ('
DIR /A-D /O-N /B "%_PathToCheck%\%_FileGlob%"
') DO (
IF /I "!_CurrentFile!" EQU "%%A-%%B" (
ECHO.Deleting: "%_PathToCheck%\%%A-%%B-%%C"
DEL /F /Q "%_PathToCheck%\%%A-%%B-%%C"
) ELSE (
ECHO.
ECHO.New File Found: "%%A-%%B"
ECHO.-----------
ECHO.Retaining: "%_PathToCheck%\%%A-%%B-%%C"
SET "_CurrentFile=%%A-%%B"
)
)
Example Output:
Y:\>Y:\t\DT.cmd
New File Found: "PLOG - File Three For yoU "
-----------
Retaining: "Y:\T\DT\PLOG - File Three For yoU - 2019.08.11 - (something) AAA 1 .xlsx"
New File Found: "PLOG - File Number Two "
-----------
Retaining: "Y:\T\DT\PLOG - File Number Two - 2019.12.19 - Ending ABDC 1111 AB.xlsx"
Deleting: "Y:\T\DT\PLOG - File Number Two - 2019.07.30 - Ending ABDC 1111 AB.xlsx"
Deleting: "Y:\T\DT\PLOG - File Number Two - 2019.03.12 - Ending Number 3 .xlsx"
New File Found: "PLOG - File Number One "
-----------
Retaining: "Y:\T\DT\PLOG - File Number One - 2020.01.01 - Ending BBB .xlsx"
Deleting: "Y:\T\DT\PLOG - File Number One - 2019.12.19 - Ending BBB 2 .xlsx"
Deleting: "Y:\T\DT\PLOG - File Number One - 2019.09.07 - Ending AAA1.xlsx"
Deleting: "Y:\T\DT\PLOG - File Number One - 2017.01.03 - Ending AAA 1 .xlsx"
Y:\>
Screenshot confirming the Script works and showing the Output and results:
Essentially this does the same thing as in my original version only now we know that we should be looking for the Hyphens
IE:
We use DIR to Sort the File names in a reversed sort order, this will mean that files that have a newer date it will appear before those with older dates.
This simplifies the logic for deleting the files, and is the crux of my original solution as well.
Because of using that method we only need to check if the first part of the file name (the part before the date) is the same as the previous file found.
We do this by creating a variable to hold the name of the current file _CurrentFile and set it empty, so on the initial check, it will not match any file name.
If _CurrentFile matches the first part of the file name (again, the part before the date) o the file dir found, then we can safely delete it.
If _CurrentFile does not match the interesting portion of the file reported by the DIR cmd, then we update the _CurrentFile variable to that new value and move on to the next file result to test.
As you are unfamiliar with cmd/batch scripting, I would like to take a minute to go into more detail about what the script is doing and why for you so you can go forward yourself:
First I should note that we have a few options on how to iterate the files, most commonly for, for/F, and For files are common go-to for looping over files, sometimes with a DIR cmd in a for /F alternatively with a WMIC file list (although, thankfully WMIC is finally getting deprecated in favor of Powershell).
As we know you simply wat to Choose based off its Filename and the date stored in the file name, then using a dir cmd to sort by Name will be a pragmatic method to do the matching quickly
Now onto what each part of the script is doing
#(
Parenthesis create code blocks in CMD and Batch Script, everything within a given Parenthesis will be evaluated at the same time.
By placing an # in front of the parenthesis any commands with it ( And not within further parenthesis, or after a DO ) will not be echoed to the screen. This is to stop this section form showing up and cluttering output.
SetLocal EnableDelayedExpansion
We are turning on Delayed Expansion, to allow us to easily evaluate the contents of the variables inside of a for loop by referencing them with !_var! instead of %_Var%, technically we can get away without this if any of your filenames have ! in them, we should disable this and re-write it a bit, if not then it's fine.
ECHO OFF
I am stopping the script from echoing every line it is doing so we have less cluttered output. Setting this command means I no longer have to use # in front of further commands within this code block or future code outside this block.
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=PLOG - * - ????.??.?? - *.xlsx"
SET "_CurrentFile="
)
Setting the variables and closing the code block with a closing parenthesis seems self-explanatory except for one _FileGlob
This is a standard File Glob it is used to match the name of the file you want to have considered for comparison.
* matches any character any number of times, ? matches any character once.
This ensures that if we encounter files which don't conform to the format we expect we can skip them.
If the need required a more explicit matching, we might use a glob of *.xlsx and use FINDStr to check against a regex pattern to make sure the format was very exactly the one needed.
in this next part
FOR /F "Tokens=1-2* Delims=-" %%A IN ('
DIR /A-D /O-N /B "%_PathToCheck%\%_FileGlob%"
') DO (
[Code]
)
Now I am going to go a little out of order here:
We are using DIR to quickly sort the files by Their Name in reverse order and return just the filenames. DIR is extremely fast at doing this, so it's preferable if you are doing a little sorting rather than matching the files using IF compares later. We utilize the file glob as mentioned above to ensure only files we want to evaluate are returned.
The Option /A-D ignores directories, /B will only output the file name (since we aren't recursing) Then, we have /O-N -- /O is "Order By" the Option N sorts by Name ascending, while -N sorts by name in Reverse (Descending) Order (IE Z-A 9-0), so we can be assured that the file with the name that has the newest date will be the first one we find.
This is all placed inside a For /F Loop which is a way to parse the output of a command. We use Delims=- to "Tokenize" or Split-up the strings FOR is receiving from the DIR command. We Tell FOR what Variable names to store the Tokes in using %%A (Variables are as follows: "? # A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ]" OR "_ `` a b c d e f g h i j k l m n o p q r s t u v w x y z" { ( More info here https://ss64.com/nt/for_f.html ) ), Variables we be assigned to Tokens starting with the one you chose.
When we specify the tokens to pick, Tokens=1-2*", specifically 1-2 means to take the first Token through the second token, and store them in the First N variables (where N = the number of variables in the set 1-2, ie %%A and %%B for our purposes), and * means stop tokenizing anything after any tokens mentioned prior to this point, and place all of the remaining portions of the line into the next variable (%%C).
Because we are tokenizing use the Hyphen as a delimiter, we now that the first two tokens will be PLOG and [Name to Compare while the date and the rest of the file name will be in the 3rd token.
In the DO ( ) section we are going to go on and process the info returned by each line and stored in our tokens.
Lets go on to examine the code within the DO ( )
IF /I "!_CurrentFile!" EQU "%%A-%%B" (
ECHO.Deleting: "%_PathToCheck%\%%A-%%B-%%C"
DEL /F /Q "%_PathToCheck%\%%A-%%B-%%C"
) ELSE (
ECHO.
ECHO.New File Found: "%%A-%%B"
ECHO.-----------
ECHO.Retaining: "%_PathToCheck%\%%A-%%B-%%C"
SET "_CurrentFile=%%A-%%B"
)
This is probably familiar to you enough as you are used to VBA, but we are testing the value of the variable _CurrentFile to the First Two Portions of the string, which we know are the entire portion of the file name up to the Date, and we need to add the Hyphen back in because when FOR splits by tokens it removes those tokens.
We check is the _CurrentFile variable ia a match for the currently returned file name's portion up to, but not including the date.
If this matches, we delete (Del) the file because we have already seen the file once before so this is one that is older.
We use the /F Option to Force deleting read-Only Files, and we use /Q to stop it from prompting us to confirm the deletion of each file.
We also ECHO. that we are deleting the file we found to note what the script is doing.
) ELSE (
If this does not match, that means this is a new file we haven't encountered for, and must be the first one returned, in which case we want to keep it because we know from the sort of Dir that it will be the interesting file.
Therefore on a non-match, we change the _CurrentFile variable to hold the value of the first two tokens %%A-%%B to use in future checks of the results returned.
We also ECHO. that we found the file and are retaining it to give a nice little indicator of what the script is doing.
A further note on ECHO -- Although I like how Echo. looks, ECHO( is safer to use, and I prefer it for that reason, but it is more confusing for folks who are unfamiliar with cmd scripts as the Open parenthesis looks like I have either a typo or an unclosed code block and can lead to people thinking it causes some problem. So for this reaosn, I try to avoid using ECHO( in favor of ECHO. when ECHO. will do.
Original Post and Versions Which used the incorrect format
You can make this a quite simple script that basically finds each unique name and keeps the 1st one so long as your names are in YYYY.MM.DD.xlsx format by pre-sorting the names so that the one with the newest date in the name one is always the first file encountered.
The Space is guaranteed? Optional?
to do this you need to use a FOR /F loop to parse the output from DIR ordered by (/O) Name Descending (-N)
DT.CMD:
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=* ????.??.??.xlsx"
SET "_CurrentFile="
)
FOR /F "Tokens=*" %%A IN ('DIR /A-D /O-N /B "%_PathToCheck%\%_FileGlob%"') DO (
SET "_TFile=%%~nA"
SET "_TFile=!_TFile:~0,-10!"
IF /I "!_CurrentFile!" EQU "!_TFile!" (
ECHO.Deleting: "%_PathToCheck%\%%~A"
DEL /F /Q "%_PathToCheck%\%%~A"
) ELSE (
ECHO.
ECHO.New File Found: !_TFile!
ECHO.-----------
ECHO.Retaining: "%_PathToCheck%\%%~A"
SET "_CurrentFile=!_TFile!"
)
)
We then simply need to compare the names of the files except for the Trailing YYYY.MM.DD.xlsx, and if the File is the 1st with that name we keep it, as we know it will be the newest.
If the name is a duplicate we can delete it because we know we already skipped the newest.
Example Output:
Y:\>Y:\t\DT.cmd
New File Found: bananas
-----------
Retaining: "Y:\T\DT\bananas 2019.07.01.xlsx"
New File Found: oranges
-----------
Retaining: "Y:\T\DT\oranges 2019.09.01.xlsx"
Deleting: "Y:\T\DT\oranges 2019.07.11.xlsx"
New File Found: apples
-----------
Retaining: "Y:\T\DT\apples 2019.07.07.xlsx"
Deleting: "Y:\T\DT\apples 2019.07.01.xlsx"
If your Date format is instead YYYY.DD.MM.Xlsx
Then you will need to go through an extra hoop or two.
Essentially in that scenario, we can do the following:
save the File name as a variable with the corrected (sortable) version of the file name (YYYY.MM.DD format) and then sort it and then compare the array of variables, deleting the ones which are not newest.
Here is that version DT_DM.CMD:
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=* ????.??.??.xlsx"
SET "_CurrentFile="
SET "_MatchList= "
)
FOR /F "Tokens=*" %%A IN ('DIR /A-D /ON /B "%_PathToCheck%\%_FileGlob%"') DO (
SET "_TFile=%%~nA"
SET "_TFileMD=!_TFile:~-5!"
SET "_TVar=__!_TFile:~0,-5!!_TFileMD:~-2!.!_TFileMD:~0,2!"
REM ECHO.Storing File: "%%~A" As: "!_TVar!"
SET "!_TVar!=%%~A"
IF /I "!_CurrentFile!" NEQ "!_TFile:~0,-10!" (
ECHO.New File Found, Adding to Sort List: "!_TFile:~0,-10!"
SET "_CurrentFile=!_TFile:~0,-10!"
SET "_MatchList=!_MatchList! "__!_TFile:~0,-10!""
)
)
ECHO.
ECHO.Delete Old Files
ECHO.-----------------
REM Loop the Matched Files:
FOR %%a IN (%_MatchList%) DO (
ECHO.
ECHO.Delete Old %%a Files
ECHO.-----------------
REM Loop the SET sorted for each File Found and Skip the First one (Newest), deleting the others.
FOR /F "Skip=1 Tokens=1-2 Delims==" %%A IN ('SET "%%~a" ^| SORT /R') DO (
ECHO.Deleting: "%_PathToCheck%\%%~B"
DEL /F /Q "%_PathToCheck%\%%~B"
REM Remove the deleted file variable so we can print a list of retained files at the end:
SET "%%A="
)
)
ECHO.
ECHO.Retained Files:
ECHO.-----------------
FOR %%a IN (%_MatchList%) DO ( SET "%%~a" )
Here is example output from that:
Y:\>Y:\t\DT_DM.cmd
New File Found, Adding to Sort List: "apples "
New File Found, Adding to Sort List: "bananas "
New File Found, Adding to Sort List: "oranges "
Delete Old Files
-----------------
Delete Old "__apples " Files
-----------------
Deleting: "Y:\T\DT\apples 2019.07.07.xlsx"
Deleting: "Y:\T\DT\apples 2019.12.01.xlsx"
Delete Old "__bananas " Files
-----------------
Delete Old "__oranges " Files
-----------------
Retained Files:
-----------------
__apples 2019.12.01=apples 2019.01.12.xlsx
__bananas 2019.01.07=bananas 2019.07.01.xlsx
__oranges 2019.11.07=oranges 2019.07.11.xlsx
Now, both of these examples Assume that you ALWAYS want whatever file is Named with the Newest Date, not the most recently modified file
This is probably the case as I know I usually want to have that scenario when working with my own dated files, in case someone or some process came along and modified the files, or I saved more than one out of order.
But just in case you really wanted to just retain the most recently modified file, we can Use the same concept as in the second version and save the Real Modified time to the Variables instead of the date on them.
DT_Modified.CMD:
#(
SetLocal EnableDelayedExpansion
ECHO OFF
SET "_PathToCheck=Y:\T\DT"
SET "_FileGlob=*.xlsx"
SET "_CurrentFile="
SET "_MatchList= "
)
FOR %%A IN ("%_PathToCheck%\%_FileGlob%") DO (
ECHO.%%A| FINDStr /I " [0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.xlsx$" >NUL && (
SET "_TFile=%%~nA"
SET "_TVar=__!_TFile:~0,-10!%%~tA"
ECHO.Storing File: "%%~A" As: "!_TVar!"
SET "!_TVar!=%%~A"
IF /I "!_CurrentFile!" NEQ "!_TFile:~0,-10!" (
ECHO.
ECHO.New File Found, Adding to Sort List: "!_TFile:~0,-10!"
ECHO.
SET "_CurrentFile=!_TFile:~0,-10!"
SET "_MatchList=!_MatchList! "__!_TFile:~0,-10!""
)
)
)
ECHO.
ECHO.Delete Old Files
ECHO.-----------------
REM Loop the Matched Files:
FOR %%a IN (%_MatchList%) DO (
ECHO.
ECHO.Delete Old %%a Files
ECHO.-----------------
REM Loop the SET sorted for each File Found and Skip the First one (Newest), deleting the others.
FOR /F "Skip=1 Tokens=1-2 Delims==" %%A IN ('SET "%%~a" ^| SORT /R') DO (
ECHO.Deleting: "%_PathToCheck%\%%~B"
DEL /F /Q "%_PathToCheck%\%%~B"
REM Remove the deleted file variable so we can print a list of retained files at the end:
SET "%%A="
)
)
ECHO.
ECHO.Retained Files:
ECHO.-----------------
FOR %%a IN (%_MatchList%) DO ( SET "%%~a" )
Example of First Script Running ad results:
Trying to find the difference. But when this powershell command is inside in the findstr, it fails. On its own, it returns the correct value. Also, without the loop, it returns the correct value.
echo:!newvalue!| findstr /R "^[0123456789][0123456789]\.[0123456789]$" >nul
if errorlevel 1 (
set newvalue=
) else (
FOR /F "usebackq delims=" %%i IN (`powershell -nop -c "'{0:n1}' -f (%newvalue% - 12.0)"`) DO (SET difference=%%i)
echo %difference%
)
Can anyone figure out what I'm missing/did wrong?
Thanks in advance.
I recommend reading How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Windows command processor replaces all environment variable references using syntax %variable% inside a command block starting with ( and ending with matching ) already on parsing the command line using this command block. This means the command line echo %difference% inside ELSE branch command block of the IF command is modified by cmd.exe before command IF is executed at all. %difference% is replaced by current value of environment variable difference or an empty string in case of environment variable difference is not defined somewhere above the IF condition. In latter case echo is the command line remaining after parsing the command block and therefore shows status of command echoing instead of the string value assigned to environment variable difference in the command line above. The solution with already enabled delayed environment variable expansion is using echo !difference! in ELSE command block.
A solution for this floating point subtraction without usage of PowerShell can be seen below:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
if defined NewValue goto Validate
:UserPrompt
set /P "NewValue=Enter value between 00.0 and 99.9: "
:Validate
echo:!NewValue!| %SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul
if errorlevel 1 set "NewValue=" & goto UserPrompt
for /F "tokens=1,2 delims=." %%I in ("%NewValue%") do set "PreComma=%%I" & set "PostComma=%%J"
set /A Difference=1%PreComma% - 112
set "Difference=%Difference%.%PostComma%"
echo Difference is: %Difference%
endlocal
After validating that the string assigned to environment variable NewValue indeed consists of two digits, a point and one more digit as requested and expected and described at How can I do a negative regex match in batch?, the floating point number string is split up on . into pre-comma and post-comma number strings.
The pre-comma number is subtracted by 12 using an arithmetic expression. But it must be taken into account that an integer number with a leading 0 is interpreted by cmd.exe on evaluation of the arithmetic expression as octal number. That is no problem for 00 to 07. But 08 and 09 would be invalid octal numbers and so Windows command processor would use value 0 resulting in a wrong subtraction result if simply set /A Difference=PreComma - 12 would have been used in batch file. The workaround is concatenating the string 1 with the pre-comma string to a number string in range 100 to 199 and subtract 112 to get the correct result.
The post-comma value does not need to be modified and so the Difference value is determined finally with concatenating the result of the arithmetic expression with the unmodified post-comma number string.
It is possible to get the Difference value also always with two digits by inserting following additional command lines above echo Difference is: %Difference%:
if %Difference:~0,1% == - (
if %Difference:~2,1% == . set "Difference=-0%Difference:~1%"
) else (
if %Difference:~1,1% == . set "Difference=0%Difference%"
)
This solution avoids also the problem that floating point result of PowerShell is formatted according to region and language settings. For example in Germany and Austria the decimal symbol is , and not . which means the subtraction result output by PowerShell for 15.3 - 12.0 is 3,3 and not 3.3.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file.
This is not technically an answer, as you've already received and accepted one a perfectly good one.
It is just to allow you to visualise a method of taking the string from your file, splitting it at the decimal point and subtracting 12, from a whole number greater or equal to 12, (see the accepted answer for whole numbers less than 12), all without 'loops' or PowerShell
#Echo Off
Rem Create a variable from the first line of your file
Set /P "newvalue="<"file.tmp"
Echo [%newvalue%]
Rem Exit if the string 'value' does not exist in '%newvalue%'
If "%newvalue%"=="%newvalue:*value=%" Exit /B
Rem ReSet the variable to everything after the string 'value'
Set "newvalue=%newvalue:*value=%"
Echo [%newvalue%]
Rem ReSet the variable to everything up to the first 'space' character
Set "newvalue=%newvalue: ="&:"%"
Echo [%newvalue%]
Rem ReSet the variable, removing the unneeded leading '=' character
Set "newvalue=%newvalue:~1%"
Echo [%newvalue%]
Rem Set a new variable to the whole number, i.e. everything up to the first '.' character
Set "whole=%newvalue:.="&:"%"
Echo [%whole%]
Rem Set a new variable to the decimal, i.e. everything after the '.' character
Set "decimal=%newvalue:*.=%"
Echo [%decimal%]
Rem Subtract 12 from the whole number
Set /A remainder=100+whole-112
Echo [%remainder%]
Rem ReJoin the variables to show the difference
Echo [%remainder%.%decimal%]
Pause
Obviously in your script proper, you'd only need:
#Echo Off
Set /P "newvalue="<"file.tmp"
If "%newvalue%"=="%newvalue:*value=%" Exit /B
Set "newvalue=%newvalue:*value=%"
Set "newvalue=%newvalue: ="&:"%"
Set "newvalue=%newvalue:~1%"
Set "whole=%newvalue:.="&:"%"
Set "decimal=%newvalue:*.=%"
Set /A remainder=100+whole-112
Echo %remainder%.%decimal%
Pause
Looping through files in a Specific Date/Time Order
Hi All,
I'm struggling to write a small addition to my batch file I have been using for a while. Firstly, here are some example files names I would be dealing with:
output.log, output.log.1, output.log.2, ..., outlput.log.199
The input file set may contain just 1 or a few of the above files (always in the order shown, newest first, oldest last), or all 200 files.
What am I attempting to do
The batch file is used to do several things with these file, such as copy them into a new directory, or create a parameter list to pass them into another command. In general, what I have works well, it did what I wanted at the time, however, processing all 200 file in a potential file set is time consuming. What I am now looking to do is limit the number of processes file, to extract just the first, say 20 files, or (if there are less than 20 files), all of them.
The problem I have is that the FOR loop I am using loops though the files by name order, not date/time order. Therefore, if I stop after 20 iterations, I end up with:
output.log, output.log.1, output.log.10, outlput.log.100, outlput.log.101, ...
I need to be able to loop through the file set in date/time order (newest first). I do not know what the date range will be, or if the files are at specific intervals, just the log file will always be as per the first list.
The Code I have
setlocal EnableDelayedExpansion
Set SnapDir=C:\Snaps\
Set SupportLog=unified_support.log
Set LogDir=\var\log
Set ParsedLogDir=\var\log\parsed
set PARAMS=
set /A NumberSupportFiles=20
Set /A StartSupportCount=0
if [%1]==[] goto :eof
:loop
rem Create a Parameter list of all the Log files, Copy File then stop after a set number of interations
for %%A in ("%SnapDir%%~n1%LogDir%\%SupportLog%*") do (
set PARAMS=!PARAMS! "%%A"
copy %%A "%SnapDir%%~n1%ParsedLogDir%\"
set /A StartSupportCount+=1
if !StartSupportCount! EQU %NumberSupportFiles% goto :jump
)
:jump
pause
As mentioned, this kinda works, it loops through the first 20 files in the file set, but the file order is by name not date/time.
From what I have read so far, any date/time manipulation appears to need an exact reference point or specific delimitation, and I can't see a way to order the set before looping. Is this possible?
You are almost done, you only need to replace the for loop by a for /F, that parses the output of dir /B A:-D /O:-D, which constitutes a list of files sorted by modification date in decending order (newest first):
#echo off
setlocal EnableExtensions EnableDelayedExpansion
Set "SnapDir=C:\Snaps\"
Set "SupportLog=unified_support.log"
Set "LogDir=\var\log"
Set "ParsedLogDir=\var\log\parsed"
set "PARAMS="
set /A NumberSupportFiles=20
Set /A StartSupportCount=0
if "%~1"=="" goto :EOF
:LOOP
rem Create a Parameter list of all the Log files,
rem Copy File then stop after a set number of interations
for /F "eol=| delims=" %%A in ('
dir /B /A:-D /O:-D "%SnapDir%%~n1%LogDir%\%SupportLog%*"
') do (
set "PARAMS=!PARAMS! "%%~A""
copy "%SnapDir%%~n1%LogDir%\%%~A" "%SnapDir%%~n1%ParsedLogDir%\"
set /A StartSupportCount+=1
if !StartSupportCount! EQU %NumberSupportFiles% goto :JUMP
)
:JUMP
pause
endlocal
exit /B
I have a series of files that have long filenames. For each filename that contains a hyphen I would like to keep the substring in position 6-8, append the _FM07_FY14.prn to the name and ignore the rest of the original filename. The new extension is now .prn. The two digits 07 stands for the month and 14 is the year. The month and year can be found from the "date created" property. Will appreciate it if you can show me how to automatically capture this mm and yy from the date created. Hardcoding this part is okay too since I can sort files by created dates and put them in separate folders.
For example
aaaaaD07.dfdd-1234.A.b.1233 new filename will be D07_FM01_FY14.prn
bbcbaA30dls-d343.a.123d new filename will be A30_FM01_FY14.prn
cdq0dG12ir3-438d.dfd.txt new filename will be G12_FM01_FY14.prn
This is the .bat file I come up with after reading many posts on here, and I don't know how to extract the mm and yy so I hard code it. I am not familiar with Powershell. I can only handle a .bat or .cmd file and run it at the command prompt. Any and all help will be highly appreciated. Thanks!
#ECHO OFF
SETLOCAL
for %%F in (*.*) do (
SET "name=%%a"
set "var=_FM01_FY14.prn"
ren *-* "%name:~6,8%var%"
)
*endlocal*
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\one"
PUSHD %sourcedir%
FOR /f "delims=" %%a IN (
'dir /b /a-d "*" '
) DO (
SET name=%%a
SET fdate=%%~ta
ECHO(REN "%%a" "!name:~5,3!_FM!fdate:~3,2!_FY!fdate:~8,2!.prn"
)
popd
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The format that I use for date is dd/mm/yyyy If yours is different, then you'll need to change the offset in the !fdate:~m,2! phrases. The value of m is the offset into the date string from the first character (the second parameter is the number of characters to select.)
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
I have a set of base filenames, for each name 'f' there are exactly two files, 'f.in' and 'f.out'. I want to write a batch file (in Windows XP) which goes through all the filenames, for each one it should:
Display the base name 'f'
Perform an action on 'f.in'
Perform another action on 'f.out'
I don't have any way to list the set of base filenames, other than to search for *.in (or *.out) for example.
Assuming you have two programs that process the two files, process_in.exe and process_out.exe:
for %%f in (*.in) do (
echo %%~nf
process_in "%%~nf.in"
process_out "%%~nf.out"
)
%%~nf is a substitution modifier, that expands %f to a file name only.
See other modifiers in https://technet.microsoft.com/en-us/library/bb490909.aspx (midway down the page) or just in the next answer.
You can use this line to print the contents of your desktop:
FOR %%I in (C:\windows\desktop\*.*) DO echo %%I
Once you have the %%I variable it's easy to perform a command on it (just replace the word echo with your program)
In addition, substitution of FOR variable references has been enhanced
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only (directory with \)
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
https://ss64.com/nt/syntax-args.html
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
You can get the full documentation by typing FOR /?
Easiest way, as I see it, is to use a for loop that calls a second batch file for processing, passing that second file the base name.
According to the for /? help, basename can be extracted using the nifty ~n option. So, the base script would read:
for %%f in (*.in) do call process.cmd %%~nf
Then, in process.cmd, assume that %0 contains the base name and act accordingly. For example:
echo The file is %0
copy %0.in %0.out
ren %0.out monkeys_are_cool.txt
There might be a better way to do this in one script, but I've always been a bit hazy on how to pull of multiple commands in a single for loop in a batch file.
EDIT: That's fantastic! I had somehow missed the page in the docs that showed that you could do multi-line blocks in a FOR loop. I am going to go have to go back and rewrite some batch files now...
Expanding on Nathans post. The following will do the job lot in one batch file.
#echo off
if %1.==Sub. goto %2
for %%f in (*.in) do call %0 Sub action %%~nf
goto end
:action
echo The file is %3
copy %3.in %3.out
ren %3.out monkeys_are_cool.txt
:end
There is a tool usually used in MS Servers (as far as I can remember) called forfiles:
The link above contains help as well as a link to the microsoft download page.
The code below filters filenames starting with given substring. It could be changed to fit different needs by working on subfname substring extraction and IF statement:
echo off
rem filter all files not starting with the prefix 'dat'
setlocal enabledelayedexpansion
FOR /R your-folder-fullpath %%F IN (*.*) DO (
set fname=%%~nF
set subfname=!fname:~0,3!
IF NOT "!subfname!" == "dat" echo "%%F"
)
pause
Echoing f.in and f.out will seperate the concept of what to loop and what not to loop when used in a for /f loop.
::Get the files seperated
echo f.in>files_to_pass_through.txt
echo f.out>>files_to_pass_through.txt
for /F %%a in (files_to_pass_through.txt) do (
for /R %%b in (*.*) do (
if "%%a" NEQ "%%b" (
echo %%b>>dont_pass_through_these.txt
)
)
)
::I'm assuming the base name is the whole string "f".
::If I'm right then all the files begin with "f".
::So all you have to do is display "f". right?
::But that would be too easy.
::Let's do this the right way.
for /f %%C in (dont_pass_through_these.txt)
::displays the filename and not the extention
echo %~nC
)
Although you didn't ask, a good way to pass commands into f.in and f.out would be to...
for /F %%D "tokens=*" in (dont_pass_through_these.txt) do (
for /F %%E in (%%D) do (
start /wait %%E
)
)
A link to all the Windows XP commands:link
I apologize if I did not answer this correctly. The question was very hard for me to read.