Solved Trigger an auto upload scrip every x seconds

June 19, 2020 at 03:41:58
Specs: Windows 10
The two scrips i have work separately but when i try to run them together it just doesnt seem to work.

@echo off
cls
:start
ECHO FOR /F "TOKENS=*" %%a IN (M:\__Test\Z.txt) DO (
echo gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
TIMEOUT /T 3 /NOBREAK
goto start
)

Ive set the time to 3 seconds just for testing


See More: Trigger an auto upload scrip every x seconds

Reply ↓  Report •

✔ Best Answer
June 25, 2020 at 01:26:12
Maybe I misunderstood, but here is some documentation on how to use gcloud init from powershell:

https://codelabs.developers.google....

https://cloud.google.com/sdk/docs/s...

and little script I threw together to upload files to a new google cloud bucket(only gets files created last 24 hours).

# LOG IN TO YOUR GOOGLE CLOUD
gcloud init

# GET FILES CREATED TODAY
$NewFiles = Get-ChildItem -Path "Z:\" -Recurse | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-1) }

# CREATE TIME STAMP FOR GOOGLE CLOUD BUCKET NAME
$TimeStamp = (Get-Date).ToString('yyyy.MM.dd')

# BUCKET NAME
$Bucket = "$TimeStamp Backup"

# CREATING BUCKET
New-GcsBucket $Bucket

# LOOPING THROUGH ALL NEW FILES IN Z:\
foreach ($File in $NewFiles) {

# GETS THE FILE NAME AND FULL PATH
$FileWithFullPath = $File.FullName

# UPLOADING NEW FILE TO BUCKET
New-GcsObject -Bucket $Bucket -File $FileWithFullPath

}



#1
June 19, 2020 at 03:48:06
Get rid of the echo command and try like that :

@echo off
cls
:start
FOR /F "TOKENS=*" %%a IN (M:\__Test\Z.txt) DO (
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
TIMEOUT /T 3 /NOBREAK
goto start
)


Reply ↓  Report •

#2
June 19, 2020 at 05:21:42
the first part gsutil -m cp -n -r Z:\%%a gs://z_backup/Z: works but it doesnt goto start (loop) afterwards

Reply ↓  Report •

#3
June 19, 2020 at 07:17:05
Try like this :
@echo off
:Main
cls
echo    Working ....
@FOR /F "TOKENS=*" %%a IN (M:\__Test\Z.txt) DO (
	echo gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
)
TIMEOUT /T 3 /NOBREAK
goto :Main


Reply ↓  Report •

Related Solutions

#4
June 19, 2020 at 08:58:28
Now the first part that worked does. this is the issue ive been having. once one part works and i change it to get the second part to work it doesnt. i tried to link the timer to the second batch file and trigger it that way but it wouldnt trigger for some reason

Reply ↓  Report •

#5
June 19, 2020 at 21:45:07
Just something to try, not a solution, try calling a sub, and try "start" command:
I changed 'start' label to 'main' to avoid confusion with start command

@echo off
cls
:main
FOR /F "TOKENS=*" %%a IN (M:\__Test\Z.txt) DO call :gs
echo returned from sub gs
TIMEOUT /T 3 /NOBREAK
goto :main

:gs
echo gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
rem NOT using 'start', enabled here
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
goto :eof
rem OR, try start:
start /wait gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
goto :eof

::----------- end test
Not much confidence I have, but worth a quick try...


Reply ↓  Report •

#6
June 22, 2020 at 07:58:52
This is the error i get as a result of previous batch script

gsutil -m cp -n -r Z:\%a gs://z_backup/Z:
The system cannot find the batch label specified - gs


Reply ↓  Report •

#7
June 23, 2020 at 13:26:18
What exactly are you trying to do, and does it have to be batch?

Reply ↓  Report •

#8
June 24, 2020 at 03:32:10
Ok so originally i had this batch script which reads a file list of work created then this one uploads it to google cloud.

ECHO FOR /F "TOKENS=*" %%a IN (M:\__Test\Z.txt) DO (
echo gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:

In the long run id like this script:

@echo off & setlocal
set file="M:\__Test\Z.txt"
(for /f "tokens=1* delims=[]" %%a in ('dir /b /o-d Z:\ ^|find /n /v ""') do (
if %%a gtr 20 goto :eof
echo %%b
)
) > %file%

to create a list based on the number of folder that have changes in them rather than based on an arbitrary number.

For the top one i wasnt it to be able to run every x hours

No it doesnt have to be batch script, it just seemed like the easiest at the time


Reply ↓  Report •

#9
June 24, 2020 at 22:23:22
I have used the "A" attribute to filter new/newly altered files. It's a very handy tool for filtering files for any reason, but this is what it was aimed at.
And you might base your script on a scheduled task rather than have it keep repeating: schtasks /create /?
gives the specifics for creating a regularly scheduled job.
Once your script is scheduled, it will repeat on interval, in background. The script only needs to select files with "A" attribute, copy them, then switch off the "A" attribute.
Any new files, or files altered in meantime, will have the "A" switched ON.
::----------- begin scheduled script
@echo off
FOR /F "TOKENS=*" %%a IN ('dir /b /aa M:\__Test\Z.txt') DO (
rem echo gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
rem see below. Should be? gsutil -m cp -n -r %%a gs://z_backup/Z:
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
:: optional: log to file: >> bakup.log echo %time% %%a
:: switch the A attribute off after copying
attrib -a %%a
)
::--------- end scheduled bat-script
My only other confusion is the paths. You have "dir M:\__test\Z.txt" but the copy has, (I think) target "Z:\M:\__test\z.txt" which violates most drive-letter-path syntax.
I WILL ASSUME that M:\__test\ is your source-drive-path. So why do you put Z:\ in front of that path? The backup is Z:, but should not be anywhere in the source?
If that's the case, then:
gsutil -m cp -n -r Z:\%%a gs://z_backup/Z:
should be:
gsutil -m cp -n -r %%a gs://z_backup/Z:

message edited by nbrane


Reply ↓  Report •

#10
June 25, 2020 at 01:26:12
✔ Best Answer
Maybe I misunderstood, but here is some documentation on how to use gcloud init from powershell:

https://codelabs.developers.google....

https://cloud.google.com/sdk/docs/s...

and little script I threw together to upload files to a new google cloud bucket(only gets files created last 24 hours).

# LOG IN TO YOUR GOOGLE CLOUD
gcloud init

# GET FILES CREATED TODAY
$NewFiles = Get-ChildItem -Path "Z:\" -Recurse | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-1) }

# CREATE TIME STAMP FOR GOOGLE CLOUD BUCKET NAME
$TimeStamp = (Get-Date).ToString('yyyy.MM.dd')

# BUCKET NAME
$Bucket = "$TimeStamp Backup"

# CREATING BUCKET
New-GcsBucket $Bucket

# LOOPING THROUGH ALL NEW FILES IN Z:\
foreach ($File in $NewFiles) {

# GETS THE FILE NAME AND FULL PATH
$FileWithFullPath = $File.FullName

# UPLOADING NEW FILE TO BUCKET
New-GcsObject -Bucket $Bucket -File $FileWithFullPath

}


Reply ↓  Report •

Ask Question