If exists, rename archive.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vairiks123
Posts: 6
Joined: 24 Jul 2016 08:49

If exists, rename archive.

#1 Post by vairiks123 » 16 Aug 2016 23:53

Good day! So, I recently made post in SO, becouse I had problems with my Archivating system. One of the kind users answered me, but for some reason code still didn't work. Since, now I am not complete noob, I changed code a bit, but it still did not do, what it was suposed to, so I came here for help. Here is my code:

Code: Select all

Echo off
set YEAR=%DATE:~6,4%
set "last=0"
set "filename=C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%.7z"
cd C:\Users\PP_lemev\Desktop\Newfolder
if not exist "C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%" mkdir "C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%"
for /d %%X in (01_%YEAR%) do "C:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
if exist "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder\01_%YEAR%.7z" (
    for /R %%i in ("C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%(*).7z") do (
       for /F "tokens=2 delims=(^)" %%a in ("%%i") do set "last=%%a"
    )
    set/a last+=1
    set "filename=C:\User\PP_lemev\Desktop\Newfolder\01_%YEAR%(%last%).7z"
)
mkdir "C:
pause   
MOVE "%filename%" "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder"


So, basicly my problem is, When I am done making archive and send it to another folder, everything works fine. But If run my script again, It will replace first archive, and "important" data will be lost. So, this script checks if Archive exists, and if it does, it renames new archive and only then moves it to new folder. New archive should look like this in the end 01_2016(1).7z.

Error output:
The system cannot find the path specified.

Any Ideas how to fix this?
Thank you for any help!

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: If exists, rename archive.

#2 Post by pieh-ejdsch » 17 Aug 2016 06:00

To what you create the variable if you do not take them

Code: Select all

set "filename=C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%.7z"

?
instead of

Code: Select all

cd C:\Users\PP_lemev\Desktop\Newfolder

take this

Code: Select all

set "Source=C:\Users\PP_lemev\Desktop\Newfolder"
set "file=01_%YEAR%"
if not exist "%Source%\%file%" md "%Source%\%file%"
PushD "%source%"
"C:\Program Files\7-Zip\7z.exe" a "%file%.7z" "%file%\"
for /f %%i in ('^(cd^&dir /s /b "%file%(*).7z" ^|find /v "" /c ') do set "now=%%i"

Where would you want the file then there? Or should this be also / only renamed?


and at end of Batch

Code: Select all

PopD


This should be wrong (it will not create a new Folder)

Code: Select all

mkdir "C:
pause   
MOVE "%filename%" "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder"


Try this

Code: Select all

mkdir Newfolder   
MOVE "%filename%" "Newfolder\"
PopD


Phil

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: If exists, rename archive.

#3 Post by Squashman » 17 Aug 2016 06:39

Why is New_Folder listed twice in this path?

Code: Select all

if exist "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder\01_%YEAR%.7z" 

vairiks123
Posts: 6
Joined: 24 Jul 2016 08:49

Re: If exists, rename archive.

#4 Post by vairiks123 » 17 Aug 2016 06:42

Squashman wrote:Why is New_Folder listed twice in this path?

Code: Select all

if exist "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder\01_%YEAR%.7z" 


It's because I have Newfolder inside Newfolder. This is just test script, but with main functionality. Real paths are used in my "Real script". Can this path have something to do with my problem?

vairiks123
Posts: 6
Joined: 24 Jul 2016 08:49

Re: If exists, rename archive.

#5 Post by vairiks123 » 17 Aug 2016 06:51

pieh-ejdsch wrote:To what you create the variable if you do not take them

Code: Select all

set "filename=C:\Users\PP_lemev\Desktop\Newfolder\01_%YEAR%.7z"

?
instead of

Code: Select all

cd C:\Users\PP_lemev\Desktop\Newfolder

take this

Code: Select all

set "Source=C:\Users\PP_lemev\Desktop\Newfolder"
set "file=01_%YEAR%"
if not exist "%Source%\%file%" md "%Source%\%file%"
PushD "%source%"
"C:\Program Files\7-Zip\7z.exe" a "%file%.7z" "%file%\"
for /f %%i in ('^(cd^&dir /s /b "%file%(*).7z" ^|find /v "" /c ') do set "now=%%i"

Where would you want the file then there? Or should this be also / only renamed?


and at end of Batch

Code: Select all

PopD


This should be wrong (it will not create a new Folder)

Code: Select all

mkdir "C:
pause   
MOVE "%filename%" "C:\Users\PP_lemev\Desktop\Newfolder\Newfolder"


Try this

Code: Select all

mkdir Newfolder   
MOVE "%filename%" "Newfolder\"
PopD


Phil


Hey Phil, thank you for your help, unfortunately I am facing problem. I gues I missed something, becouse I don't fully understand this code? Could you please explain it, in-depth? Thank you.

Here is what I wrote:

Code: Select all

Echo off
set YEAR=%DATE:~6,4%

set "Source=C:\Users\PP_lemev\Desktop\Newfolder"
set "file=01_%YEAR%"
if not exist "%Source%\%file%" md "%Source%\%file%"
PushD "%source%"
"C:\Program Files\7-Zip\7z.exe" a "%file%.7z" "%file%\"
for /f %%i in ('^(cd^&dir /s /b "%file%(*).7z" ^|find /v "" /c ') do set "now=%%i"
MOVE "%filename%" "Newfolder"
PopD

Post Reply