Little help with Windows batch

ggadrian

Senior member
May 23, 2013
270
0
76
Hi, I'm trying to write a batch that has to do the following:

-Delete A: \temp
-Create A: \temp
-Copy newest backup file from B: \backup to A: \temp
-Rename file with: backup_%DATE%
-Encrypt with Axcrypt
-Send using Filezilla

So far I got this:

:: DELETE OLD FILES
rd /s /q A: \temp

::CREATE TEMP FOLDER
md A: \temp

::COPY LAST BACKUP TO TEMP
Robocopy "B: \backup" A: \temp /MaxAge:1

The problem is that Robocopy is copying the las 2 files (I do 1 backup per day). I need to find a way to only copy the newest file or to delete the old one after being copied.

I'm pretty stuck here and I can't continue with the script if I can't solve it.

Thanks.

P.S.: I'm not really sure if that question should go here or in Windows, sorry if I posted it in the wrong place.
 

debian0001

Senior member
Jun 8, 2012
464
0
76
You should really be using PowerShell.. Batch is so old and useless.

PHP:
Remove-Item A:\Temp

New-Item -ItemType directory -Path A:\Temp

$day = (Get-Date).AddDays(-1)

Delete-Item C:\backup\* | Where-Object { $_.LastWriteTime -lt $day }

Copy-Item C:\backup\* A:\temp

I'm not going to do it all for you but each piece is very easy to do with PowerShell
 

ggadrian

Senior member
May 23, 2013
270
0
76
You should really be using PowerShell.. Batch is so old and useless.

Remove-Item A:\Temp

New-Item -ItemType directory -Path A:\Temp

$day = (Get-Date).AddDays(-1)

Delete-Item C:\backup\* | Where-Object { $_.LastWriteTime -lt $day }

Copy-Item C:\backup\* A:\temp

I'm not going to do it all for you but each piece is very easy to do with PowerShell

So that's what I should do in PowerShell?

For what I wan't to achieve batch is not fit?

Thanks

(I don't want you guys to do it for me, actually I enjoy doing things like that and learning)
 

debian0001

Senior member
Jun 8, 2012
464
0
76
PowerShell actually has easy logic, whereas Batch is archaic and really not that good even in its prime.
 

ggadrian

Senior member
May 23, 2013
270
0
76
Ok, just updated the server to PowerShell3.0, tomorrow I'll look into the sintaxis and how to make it work.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
While I agree batch is pretty archaic, it should easily be able to do what's listed above.

You can copy and rename a file at the same time:

Code:
copy \temp\somefile.txt \temp2\newname.txt

The above copies somefile.txt from temp to temp2 but names it newname.txt.

The only parts I'd actually have to look up is using program command lines to do encrypting and ftp. There are built in FTP commands in batch though. Not sure if you actually require Filezilla.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Actually what I used to do at my previous job is use C++ to execute batch commands to make functionality easier. Use C++ to do the grunt work and batch commands to execute.
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
It seems like robocopy is overkill for this situation when all you really want to do is sort your files by date.

Use dir with the appropriate flags to sort the files in the directory, then move it rather than copy it. It will take care of you needing to remove it in the next step.

Here's a quick and dirty loop that will move the newest file to a new location and rename it to a new name with the date in it.

Code:
FOR /F %%A IN ('dir /a-d /o:-d /b') DO (
        move %%A a:\temp\backup_%date%
        GOTO :eof
)
 

ggadrian

Senior member
May 23, 2013
270
0
76
It seems like robocopy is overkill for this situation when all you really want to do is sort your files by date.

Use dir with the appropriate flags to sort the files in the directory, then move it rather than copy it. It will take care of you needing to remove it in the next step.

Here's a quick and dirty loop that will move the newest file to a new location and rename it to a new name with the date in it.

Code:
FOR /F %%A IN ('dir /a-d /o:-d /b') DO (
        move %%A a:\temp\backup_%date%
        GOTO :eof
)

Thank you.

I have a question:

Do I have to change directory to the directory where the backups are and then I run this loop? Or should I change where it says dir for the directory where the backups are?

I tried both and it didn't work, but provably because I missed something or because I wrote something wrong.
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
You can change directory (cd) to the location of the backups before the loop.

Or, you can change the command that the loop is running to include the directory that you want to run the dir command on. See the "backups" path in the modified first line of code below.

Code:
FOR /F %%A IN ('dir /a-d /o:-d /b b:\backups') DO (
        move b:\backups\%%A a:\temp\backup_%date%
        GOTO :eof
)

All that the loop is doing is stepping through the list of files that the dir command shows and storing that filename in the variable %%A. If you want to practice, you can see what the command will show by simply typing it at a command prompt. Each filename will be stored line-by-line in %%A.

The dir command sorts the files by date for the loop. So the loop stores the first value, does the move command using that first value. However, since you only wanted one item, that's why there's a GOTO line right after the move command. It's a quick way to jump out of the loop after the first item is moved.
 
Last edited:

ggadrian

Senior member
May 23, 2013
270
0
76
You can change directory (cd) to the location of the backups before the loop.

Or, you can change the command that the loop is running to include the directory that you want to run the dir command on. See the "backups" path in the modified first line of code below.

Code:
FOR /F %%A IN ('dir /a-d /o:-d /b b:\backups') DO (
        move b:\backups\%%A a:\temp\backup_%date%
        GOTO :eof
)

All that the loop is doing is stepping through the list of files that the dir command shows and storing that filename in the variable %%A. If you want to practice, you can see what the command will show by simply typing it at a command prompt. Each filename will be stored line-by-line in %%A.

The dir command sorts the files by date for the loop. So the loop stores the first value, does the move command using that first value. However, since you only wanted one item, that's why there's a GOTO line right after the move command. It's a quick way to jump out of the loop after the first item is moved.

Thank you, that was really useful.

Now that I've finished the batch file for the windows machine I have to start thinking about a linux script or something for the ftp server.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |