Batch file required to move rars and zips by any inside file date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Batch file required to move rars and zips by any inside file date

#1 Post by val5662 » 17 Jun 2017 10:39

Hi all...
Please help if you can.
I need a batch file to search inside over 100 rar and zip files and move the rar or zip files to a folder according to a year of any file inside.Here is what I tried last.....didn't work....

@ echo off
md 2016files
echo IN PROGRESS PLEASE WAIT
for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /m /i /e "2016" >NUL && move "%%F" 2016files >NUL
echo rar and zip files with any file dated 2016 moved to 2016files folder successfully
pause
exit /b

Thanks!
Val

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file required to move rars and zips by any inside file date

#2 Post by thefeduke » 23 Jun 2017 22:52

You sort of have the right idea and I had this third party software installed so you can try this untested code. It requires the year as the first argument.

Code: Select all

@Echo Off & SetLocal
Set "year=%~1"
md %year%files
echo IN PROGRESS PLEASE WAIT
    for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /c:"^%year%-" >NUL && echo "%%F" has %year% data || echo "%%F" skipped
rem for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /c:"^%year%-" >NUL && move "%%F" 2016files >NUL
rem echo rar and zip files with any file dated 2016 moved to 2016files folder successfully
    echo rar and zip files with any file dated %year% classified successfully
pause
exit /b
If you are satisfied, swap the remarked code lines to enable your move code. You attempted to use the /M and /e tags of findstr inappropriately. On examining the output of this utility, I found the year to be at the beginning of the line. The /m tag is for a filename that you are not actually searching. Findstr is only being passed a stream of data from the utility. Also, please use code tag when posting code.

I do not share your environment as to file and script file location, so I tested using this:

Code: Select all

@Echo Off & SetLocal
 
Set "year=%~1"
 
for %%F in (%userprofile%\Downloads\*.zip) do (
    "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /C:"^%year%-" >nul && Echo "%%F" has %year% data || Echo "%%F" skipped   
)
 
exit /b                                                           

John A.

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch file required to move rars and zips by any inside file date

#3 Post by val5662 » 11 Jul 2017 21:31

thefeduke
Sorry for the late reply....been super busy.
Sorry but I am not very good at batch files and terminology.
I don't understand what you mean by "please use code tag when posting code."
What is a "code tag"?

Is this the correct way of writing this batch file when you said "It requires the year as the first argument."

The code I tried is this one you gave me( I replaed the first "year" word with the 2016 as shown:

@Echo Off & SetLocal
Set "2016=%~1"
md %year%files
echo IN PROGRESS PLEASE WAIT
for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /c:"^%year%-" >NUL && echo "%%F" has %year% data || echo "%%F" skipped
rem for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /c:"^%year%-" >NUL && move "%%F" 2016files >NUL
rem echo rar and zip files with any file dated 2016 moved to 2016files folder successfully
echo rar and zip files with any file dated %year% classified successfully
pause
exit /b

All this does it says data found, BUT it finds ALL files that were made in other years than 2016 also.

I did another test in a test folder with a few copied rar and zip files,removed the "rem" words,and it made 1 file with no file extension and removed all the test files into that 1 file.The rar and zips were no longer there.I added a .zip to the file that had no extension.extracted what was in there and only 1 file extracted....I had 4 files in this test folder with only 2 having been made in 2016.
Please advise...
Thanks

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: Batch file required to move rars and zips by any inside file date

#4 Post by batnoob » 12 Jul 2017 09:23

a code tag is [ code ]
your code here
[ /code ]
for example.

Code: Select all

this is some code

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file required to move rars and zips by any inside file date

#5 Post by thefeduke » 12 Jul 2017 11:43

val5662 wrote:Is this the correct way of writing this batch file when you said "It requires the year as the first argument."

The code I tried is this one you gave me( I replaed the first "year" word with the 2016 as shown:

Code: Select all

@Echo Off & SetLocal
Set "2016=%~1"
md %year%files

thefeduke wrote:

Code: Select all

@Echo Off & SetLocal
Set "year=%~1"
md %year%files
If you are satisfied, swap the remarked code lines to enable your move code.
When you were not satisfied with the results and tested the MOVE code it had to be expected that it would fail also, but thanks for the diagnostic info. My example did not generalize your move code, so here it is for consistency:

Code: Select all

rem for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F" | findstr /r /c:"^%year%-" >NUL && move "%%F" %year%files >NUL
rem echo rar and zip files with any file dated %year% moved to %year%files folder successfully

So here are the forensics: Assuming that the passed argument was "2016", my code set the variable year to 2016 allowing the directory to be created as '2106files' and the search argument to be '2016-'. Your test left the variable year undefined and set a variable named 2016 to have a value of 2016. This variable is never used later, but now the directory is created as 'files' and the search argument is '-' after the nul value of %year% is substituted. It seems that the '-' is always found and your hard-coded MOVE directory '2016files' does not exist and is treated as a new sequential file and reused, making things disappear. The overwrite switch is set to /-Y when running in a batch script or you would have been prompted for permission. Suggestion untested, but might do more now.

John A.

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch file required to move rars and zips by any inside file date

#6 Post by val5662 » 12 Jul 2017 13:02

Sorry thefeduke....
I am lost.I am not a professional batch maker or understand all the terminology you just said.
If you can please give me the exact batch coding I would need in my case.
More info if you need it:
I want to put the batch files inside a directory full of rars and zips and make a folder there called 2016files and search inside all the rars and zips to find any file that was made in 2016 and move that rar or zip file into that 2016files folder and leave all the rest alone.
Thanks!

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file required to move rars and zips by any inside file date

#7 Post by thefeduke » 12 Jul 2017 18:56

OK. I have built no check on the validity of the argument as you pass it. You need to delete the target directory when you do the REMARK swap.

Code: Select all

@Echo Off & SetLocal

Set "year=%~1"
::  The desired year can be changed in the following line before each run
If Not Defined year Set Year=2001
md %year%files

    For %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F"       | findstr /r /c:"^%year%-" >NUL && copy /-Y "%%F" /B %year%files /B || Echo "%%F" skipped
rem for %%F in (*rar,*.zip) do "C:\Program Files\7-Zip\7z.exe" l "%%F"       | findstr /r /c:"^%year%-" >NUL && move /-Y "%%F"    %year%files    || Echo "%%F" skipped

pause
exit /b
Just put this script in your directory with the zip files.

Code was edited to suit topic owner.
John A
Last edited by thefeduke on 12 Jul 2017 23:14, edited 1 time in total.

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch file required to move rars and zips by any inside file date

#8 Post by val5662 » 12 Jul 2017 22:18

Yo John A...
I am lost.Let me explain in detail.
I put the batch file in a test folder with 2 rars and 2 zips file that had files inside that were made in different years.
3 had files dated 2003.1 had files dated 2001.When I ran the test batch exactly as I copied from your last post,it copied all 4 files into a folder called files.What I wanted was just the rar or zip files (just copied and moved for this test) into the "files" folder that had only the 2003 dated files inside.It copied and moved all 4.
Was I supposed to put the 2003 date somewhere in the batch file you provided?
Thanks again.Sorry for all the questions I have......
Val

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file required to move rars and zips by any inside file date

#9 Post by thefeduke » 12 Jul 2017 23:22

val5662 wrote:Was I supposed to put the 2003 date somewhere in the batch file you provided?
OK Val. Let's do it that way. I edited my most recent post to add some code.

You can update the year value where indicated and just open the script file. It will also still run from a DOS prompt but you and I no longer need to worry about that.

John A

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch file required to move rars and zips by any inside file date

#10 Post by val5662 » 14 Jul 2017 07:53

John A....
Your last modified code with the "If Not Defined year Set Year=2001" worked 100%.
I appreciate your coding knowledge,time and patience!
Thanks a bunch! :D
Val

Post Reply