Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Please explain what this code is doing  (Read 16706 times)

0 Members and 1 Guest are viewing this topic.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #15 on: April 13, 2012, 05:15:44 PM »
I'm persisting with this because I need this task automated.
I enjoy learning.
I want it in batch.
And it's my choice...

No, I don't know any other programming languages.
I really don't want to have to install some type of add-on/framework to each and every computer I'd need this to run on.


I don't know what I need the usebackq thing either... :-\


Getting a little further now.

When it comes to moving the file and/or creating the folder on the external, I either get a syntax error or a folder created in the same folder as the batch.
Nothing whatsoever is created on the external. :-\


If I follow this (note the periods):

IF EXIST filename. (
del filename.
) ELSE (
echo filename. missing.
)

I can get the folder created on the external, but then the rest just errors out again with syntax errors. :-\

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #16 on: April 14, 2012, 12:36:21 AM »
Why don't you post your WHOLE SCRIPT?

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #18 on: April 14, 2012, 06:32:43 AM »
I did: http://www.computerhope.com/forum/index.php/topic,129856.msg844361.html#msg844361

Quote
I figured out that I had to put a usebackq thing in front of the word tokens in your script in order for it to find the drive.

That's the script I meant, my script. You must have altered it, because it works fine here, and I want to see what alterations you have made.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #19 on: April 14, 2012, 06:38:57 AM »
Code: [Select]
@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION



:RUNBACKUP

CLS
ECHO Starting backup tasks...
TIMEOUT 2 /NOBREAK >NUL

ECHO.
0>NUL SET /P=Awaiting Acronis image dump...
"C:\Program Files (x86)\Common Files\Acronis\TrueImageHome\TrueImageHomeService.exe" /script: "C:\ProgramData\Acronis\TrueImageHome\Scripts\38FAF255-2450-41CD-9795-B1BFA5EBCF0E.tib.tis"


TIMEOUT 1 /NOBREAK >NUL
IF EXIST MyBack.tib cALL :FINDEXTERNAL

ECHO Fail^^!
TIMEOUT 1 /NOBREAK >NUL

ECHO.
0>NUL SET /P=Acronis image dump incomplete...
TIMEOUT 1 /NOBREAK >NUL

ECHO.
ECHO Press any key to try again...
PAUSE >NUL

CALL :RUNBACKUP



:FINDEXTERNAL

ECHO Done^^!
TIMEOUT 2 /NOBREAK >NUL

:FINDAGAIN

SET volumeserial=30BC-F5A4
SET wantedfolder=AcronisBackups
SET backupfilename=MyBackup.tib
SET found=no

0>NUL SET /P=Locating external drive...

FOR %%A IN (D E F G H I J K L M O P Q R S T U V W X Y Z) DO (
IF EXIST %%A:\ (
FOR /F "usebackqTOKENS=1-5" %%B IN ('VOL %%A:\ ^| FIND "Volume Serial Number"') DO (
IF "%%F"=="%volumeserial%" SET %driveletter%=%%A
SET found=yes
)
)
)

IF "%found%"=="no" (
TIMEOUT 1 /NOBREAK >NUL

ECHO Fail^^!
TIMEOUT 1 /NOBREAK >NUL

ECHO.
ECHO Connect it now and cancel "Autoplay"...
TIMEOUT 1 /NOBREAK >NUL

ECHO.
ECHO Press any key to try again...
PAUSE >NUL

CALL :FINDAGAIN
)


TIMEOUT 1 /NOBREAK >NUL

ECHO Found^^!
TIMEOUT 2 /NOBREAK >NUL

0>NUL SET /P=Moving backup to external...

IF EXIST %driveletter%:\%wantedfolder% (
IF EXIST "%driveletter%:\%wantedfolder%\%backupfilename%" DEL "%driveletter%:\%wantedfolder%\%backupfilename%"
) ELSE (
MKDIR "%%driveletter:\%wantedfolder%"
)

MOVE /Y "%backupfilename%" "%driverletter%:\%wantedfolder%\"

TIMEOUT 1 /NOBREAK >NUL

ECHO Done^^!
TIMEOUT 2 /NOBREAK >NUL


ECHO.
ECHO All tasks complete^^! ;)
TIMEOUT 3 /NOBREAK >NUL

EXIT

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #20 on: April 14, 2012, 06:41:46 AM »
There should be a space between usebackq and tokens; however, you DON'T NEED usebackq there. Why did you think you did?

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #21 on: April 14, 2012, 06:44:14 AM »
There should be a space between usebackq and tokens; however, you DON'T NEED usebackq there. Why did you think you did?

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #22 on: April 14, 2012, 06:46:09 AM »
Please answer the question.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #23 on: April 14, 2012, 06:53:15 AM »
usebackq
   

Specifies that you can use quotation marks to quote file names in filenameset, a back quoted string is executed as a command, and a single quoted string is a literal string command.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #24 on: April 14, 2012, 06:54:17 AM »
usebackq
   

Specifies that you can use quotation marks to quote file names in filenameset, a back quoted string is executed as a command, and a single quoted string is a literal string command.

Because you're using "Volume Serial Number"

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #25 on: April 14, 2012, 06:59:46 AM »
"Volume Serial Number" is not a file name, it's a string parameter for FIND. You didn't use any backquotes anyway.


S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #26 on: April 14, 2012, 07:08:10 AM »
usebackq
   

Specifies that you can use quotation marks to quote file names in filenameset, a back quoted string is executed as a command, and a single quoted string is a literal string command.

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #27 on: April 14, 2012, 07:21:30 AM »
Quote
To use quotation marks, you must use usebackq. Otherwise, the quotation marks are interpreted as defining a literal string to parse.

The string is a literal string to parse. Therefore usebackq is the opposite of what is needed. Anyhow the code I supplied works fine. Here is proof.

Code: [Select]
@echo off
set volumeserial=2C51-AA7F

REM set volume found flag to NO
set found=NO

Echo.
Echo Searching for Volume Serial Number: %volumeserial%
Echo.
Echo Checking each drive:
echo.

REM for each letter of the alphabet
for %%A in (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) do (
   REM If the drive exists
   if exist %%A:\ (
      REM %%A is the driveletter we are currently checking
      REM Run the Vol command on that drive
      REM Pipe the output to FIND filtering for the Volume Serial Number line
     
      REM NO USEBACKQ
      for /f "tokens=1-5" %%B in ('Vol %%A: ^| find "Volume Serial Number"') do (
         echo Drive %%A Volume Serial Number %%F
         REM if the 5th spacedelimited token is the Volume serial number we want
         REM set the variable %driveletter% equal to %%A
         if "%%F"=="%volumeserial%" set driveletter=%%A
         REM set volume found flag to YES
         set found=YES
         )
      )
   )
Echo.   
Echo Found drive: %found%
If "%found%"=="YES" Echo The drive containg volume %volumeserial% is %driveletter%
Echo.

Code: [Select]
C:\Batch\Test\Find-Volume.bat

Searching for Volume Serial Number: 2C51-AA7F

Checking each drive:

Drive C Volume Serial Number E4DB-A92A
Drive D Volume Serial Number E8B2-C5D7
Drive F Volume Serial Number B41F-69B9
Drive S Volume Serial Number 2C51-AA7F
Drive T Volume Serial Number F0EE-2AD4
Drive W Volume Serial Number E808-63DC
Drive Y Volume Serial Number B895-6B9B

Found drive: YES
The drive containg volume 2C51-AA7F is S


S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #28 on: April 14, 2012, 09:51:18 AM »
Code: [Select]
IF EXIST "%driveletter%:\%wantedfolder%" (
IF EXIST "%driveletter%:\%wantedfolder%\%backupfilename%" DEL "%driveletter%:\%wantedfolder%\%backupfilename%"
) ELSE (
MKDIR "%%driveletter:\%wantedfolder%"
)

MOVE /Y "%backupfilename%" "%driverletter%:\%wantedfolder%\"


Fails no matter what I try. :-\

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #29 on: April 14, 2012, 11:27:52 AM »
1. Check that the variables %driveletter% %wantedfolder% and %backupfilename% expand to what you think they do
2. Check your code much more carefully (it is usually better to copy and paste code, and not to re-type it) This will avoid wasting other peoples time.

MKDIR "%%driveletter:\%wantedfolder%"
)

MOVE /Y "%backupfilename%" "%driverletter%:\%wantedfolder%\"

3. If you truly want to do coding, you need to learn (a) to check your own work (b) to be accurate, careful and thorough.
4. If you truly want others to help you, read point (3) again.
5. Read point (3) again.