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 16707 times)

0 Members and 1 Guest are viewing this topic.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Please explain what this code is doing
« on: April 10, 2012, 01:01:35 PM »
I'm working with a variation of what you see below.
I like to know if it can be simplified and, more specifically, what exactly is happening with the  VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :CONTINUE %%N portion of things.


Code: [Select]
@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION


:LOOKAGAIN

CLS
SET "SN=31FD-C3B4"
SET "found="

0>NUL SET /P=Searching for external drive...
TIMEOUT 2 /NOBREAK >NUL
FOR %%N IN (D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
IF defined found GOTO :CONTINUE
VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :CONTINUE %%N
)
ECHO  FAIL^^!
ECHO Connect it now and cancel Autoplay if prompted.
ECHO Press any key to try again...
PAUSE >NUL
GOTO :LOOKAGAIN

:CONTINUE
SET "found=yes"
ECHO  Found^^!
ECHO Do some stuff within drive %1: here...

PAUSE


EXIT


This ties in with the last few posts I've made recently.
I'm trying to fully automate, unless there's an error of course, my backup tasks.

Basically,
I've set Task Manager to run a batch file which, in turn, runs Acronis. (As opposed to Acronis running the batch as a post-command).
This way, since both the batch and Acronis would run at the same time if missed, I'm pretty much forcing the batch to wait for Acronis to finish dumping its backup image before continuing.

Now, The above will be the first part of my batch.
It will be ensuring that nothing runs until the correct external drive is connected.
As is obvious above, it's checking for correctness based off of the volume serial number just in case the drive letter changes for whatever reason.


At any rate, thanks in advance for the explanation! ;D

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #1 on: April 10, 2012, 01:37:10 PM »
You wrote it, but you don't know what it does? Did you mean you copied from websites?

Code: [Select]
@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION


:LOOKAGAIN

CLS
SET "SN=31FD-C3B4" (set the variable %SN% to the value 31FD-C3B4, obviously a volume serial number; not sure why using quotes here)
SET "found=" (set the variable %found% to null (nothing) (undefined) not sure why using quotes here too. )

0>NUL SET /P=Searching for external drive... (the set /p trick I told you about in another thread)
TIMEOUT 2 /NOBREAK >NUL  (wait 2 seconds)

for each of these drive letters...

FOR %%N IN (D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
IF defined found GOTO :CONTINUE (jump out of loop if %found% is now defined)
VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :CONTINUE %%N (pipe the output VOL [driveletter] thorough find with %sn% as the search term; if found call :CONTINUE with driveletter as parameter)
)
ECHO  FAIL^^! (you only get here if none of the drive letters had the volume serial number you specified above)
ECHO Connect it now and cancel Autoplay if prompted.
ECHO Press any key to try again...
PAUSE >NUL
GOTO :LOOKAGAIN (what all this does is too obvious to need explaining)

:CONTINUE
SET "found=yes" (%found% is now defined)
ECHO  Found^^!
ECHO Do some stuff within drive %1: here...

PAUSE


EXIT

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #2 on: April 11, 2012, 10:28:39 AM »
Did you mean you copied from websites?

I never said I wrote it, I said I'm working with a variation of it.
Yes, I copied it from somewhere...

Thanks for the reply!
I was really only interested in the VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :CONTINUE %%N portion of it all.

I know the >NUL just "redirects" stuff to some nonexistent "display/output" but I'm not comprehending the 2 in front of it.
Nor am I understanding the | pipe and the %%N at the end.

Lastly, can it be simplified, or is it already as small as it could get?


Sorry to be a pain in the *censored*, I'm just trying to understand and learn something, ya know? :-[



EDIT:
Found out what the pipe | and the 2>NUL parameters mean/do.
« Last Edit: April 11, 2012, 10:41:07 AM by S3NTYN3L »

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #3 on: April 11, 2012, 11:21:50 AM »
I was really only interested in the VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :CONTINUE %%N portion of it all.

Study topics:

Console streams: STDERR and STDOUT - redirecting them
Pipes
The FOR command
The && errorlevel operator

Plenty of Windows command language sites and tutorials on the web.





S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #4 on: April 11, 2012, 08:59:20 PM »
Agreed, but it's hard to know where to start when you don't know the names of the commands/arguments/whatever.

Thanks for the enlightenment!

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #5 on: April 13, 2012, 11:27:45 AM »
OK, I'm so utterly lost now it's not even funny.

I goofed and have the batch running from the external it was to be detecting.
If it's not connected, the batch can't run so it's pointless.

I've been trying to rewrite the batch now for the better part of 7 hours...

I'll now store the batch in the C:\ folder.
My backup will get dumped into the same folder.

AFTER the batch detects my external, whatever drive letter that may be, I now need it to check if a folder exists in that drive.
If the folder exists, delete the old backup contained therein.
If the folder doesn't exist, it needs to create the folder and then move the backup image it.

I've tried all sorts of variations of syntax for the commands CHDIR, MKDIR, IF NOT EXIST, etc, all containing the %%N variable, but I just can't get this nailed down.


Umm, so here's what I'm now working with:

Code: [Select]
@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION


:: ***RUN BACKUP***

:BACKUP

CLS
ECHO Starting Backup...
ECHO.
TIMEOUT 3 /NOBREAK >NUL

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"

IF NOT EXIST MyBackup.tib CALL :FAIL

CALL :EXTERNAL


:FAIL

ECHO Fail^^!
TIMEOUT 1 /NOBREAK >NUL

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

CALL :BACKUP



:: ***FIND EXTERNAL***

:EXTERNAL

ECHO  Done^^!
TIMEOUT 3 /NOBREAK >NUL

SET SN=30BC-F5A4
SET found=

0>NUL SET /P=Searching for external drive...
TIMEOUT 3 /NOBREAK >NUL

FOR %%N 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 defined found CALL :MOVE
VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :MOVE %%N
)

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: EXTERNAL



:: ***MOVE BACKUP***

:MOVE

SET "found=yes"
ECHO  Found^^!
TIMEOUT 3 /NOBREAK >NUL

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

IF NOT EXIST "%%N:\Acronis Backups\" MKDIR "%%N:\Acronis Backups\"
CHDIR /D "%%N:\Acronis Backups\"
IF EXIST MyBackup.tib DEL MyBackup.tib

CHDIR /D "%~dp0"
MOVE /Y MyBackup.tib "%%N:\Acronis Backups\"

0>NUL SET /P=Move complete^^!
TIMEOUT 3 /NOBREAK >NUL

ECHO  Until next time^^! ;)
TIMEOUT 3 /NOBREAK >NUL

PAUSE >NUL
EXIT



As always, thanks for the helping hand! ;D

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #6 on: April 13, 2012, 11:38:40 AM »
The %%N variable only exists within the FOR loop. It is meaningless outside it.

You need to slow down. Getting in a panic and frantically sticking together bits of code as if they were Lego bricks is not going to get you anywhere. You need to start simple and work up to scripts of the complexity you seem to want.


S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #7 on: April 13, 2012, 11:52:20 AM »
Fair enough.
I was under the impression that the variable was still available for use later in the script.


OK, so I'll need another FOR loop then, yes?

Using this as a base, I've also tried rewriting the code in the () to get the drive letter again and then see if the folder exists, but no joy.

Code: [Select]
SET SN=30BC-F5A4
SET found=

0>NUL SET /P=Searching for external drive...
TIMEOUT 3 /NOBREAK >NUL

FOR %%N 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 defined found CALL :MOVE
VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :MOVE %%N
)



I'm not in a panic, I'm just running out of syntax to try.
Google and some other sites I've found on batch commands explain the fundamentals of the various commands but are pretty lean on the more in depth stuff.

I'm trying here, lol...
Care to push mein the right direction instead of telling me to slow down?

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #8 on: April 13, 2012, 12:32:04 PM »
OK, I think I'm getting closer... :-\


Code: [Select]
SET found=

0>NUL SET /P=Moving backup to external drive...
TIMEOUT 3 /NOBREAK >NUL


FOR %%I In (D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) DO (
IF defined found CALL :DELETE
IF NOT EXIST "%%I:\Acronis Backups" MKDIR "%%I:\Acronis Backups\"
CHDIR /D "%%I:\Acronis Backups\" && CALL :DELETE %%I

:DELETE

SET "found=yes"

IF EXIST DEL test.txt
CHDIR /D "%~dp0"
MOVE /Y MyBackup.tib "%%I:\Acronis Backups\"
)

TIMEOUT 3 /NOBREAK >NUL

0>NUL SET /P=Move complete^^!
TIMEOUT 3 /NOBREAK >NUL

ECHO  Until next time^^! ;)
TIMEOUT 3 /NOBREAK >NUL

PAUSE >NUL
EXIT

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #9 on: April 13, 2012, 01:21:20 PM »
This is how I would do it.

Code: [Select]
@echo off
set volumeserial=31FD-C3B4
set wantedfolder=Acronis Backups
set backupfilename=MyBackup.tib

REM set volume found flag to NO
set found=NO

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
for /f "tokens=1-5" %%B in ('Vol %%A: ^| find "Volume Serial Number"') do (
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
)
)
)

REM if we didn't find the volume...
If "%found%"=="NO" (
Echo Volume %volumeserial% not found
Pause
Exit
)

REM if we did find the volume
REM we know the drive letter

REM if the backup folder exists on that drive...
if exist "%driveletter%:\%wantedfolder%" (
REM If there is a backup file there, delete it
if exist "%driveletter%:\%wantedfolder%\%backupfilename%" del "%driveletter%:\%wantedfolder%\%backupfilename%"
) else (
REM if it does not exist
REM create it
md "%driveletter%:\%wantedfolder%"
)

REM move the backup file to the folder
move /y "%backupfilename%" "%driveletter%:\%wantedfolder%"


S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #10 on: April 13, 2012, 02:07:38 PM »
Thanks ST.

Umm, Does it matter what you use for the letters after the %%A and %%F for example?
If so, where do you find out which does what?
Is there a reference listed somewhere?

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #11 on: April 13, 2012, 02:29:56 PM »
Thanks ST.

Umm, Does it matter what you use for the letters after the %%A and %%F for example?
If so, where do you find out which does what?
Is there a reference listed somewhere?

for /f "tokens=1-5" %%B in ('Vol %%A: ^| find "Volume Serial Number"') do (

%%B is the 1st token, %%C the second, and so on up to %%F (the 5th letter from B in the alphabet is F)

you can type FOR /? at the prompt to see the documentation. You can do this for all commands, and there is an online reference at

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true

Also see this site

http://ss64.com/nt/

You could have found all this with a little Googling.

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #12 on: April 13, 2012, 03:54:03 PM »
K...


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.

Now it won't make the folder and or the file.

I'm trying to see what's wrong, but I'm just not seeing it.

I've even tried adding PAUSEs after each command but they get ignored for some reason and the window flashes some text and closes.
It's MUCH too fast to read.

Any ideas?



EDIT:

Just did a search...
It's dumping a single file, I'm testing with a text file, into my system32 directory for some reason.
Opening it up shows me that the text is just getting repeated and repeated each time I attempt to run the batch...

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Please explain what this code is doing
« Reply #13 on: April 13, 2012, 04:24:54 PM »
OK. I am butting in again.
OP, can you please explain WHY, in the year of 2012, you need to use a complex batch file to solve a problem.
Have you mastered any other computer language?

Please don't misunderstand. If n.you must have a answer in batch, Salmon Trout can indeed give you the answer in actual Batch commands. He is a real Guru in batch. But if you ask him, he could also show you have it could have be done is a more intuitive script language that does not have the many overloaded operators of MS-DOS command language. Vb-script wold be a good choice. Its cousin C-script can integrate well with batch commands.

Just an observation. Pardon me.  :D

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #14 on: April 13, 2012, 04:50:34 PM »
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.

It worked fine for me without any usebackq. I did test it, you know.

Quote
I'm trying to see what's wrong, but I'm just not seeing it.

I've even tried adding PAUSEs after each command but they get ignored for some reason and the window flashes some text and closes.
It's MUCH too fast to read.

Any ideas?

I have to ask a question:

Batch scripting is obviously not for you. Why are you persisting with this? It isn't going anywhere.


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.



                                   

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #30 on: April 14, 2012, 11:44:06 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.                     


Got it...



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

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



Sorry, I missed the typos... :-[

Salmon Trout

  • Guest
Re: Please explain what this code is doing
« Reply #31 on: April 14, 2012, 12:05:09 PM »
So does it work now, or not?

S3NTYN3L

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: Please explain what this code is doing
« Reply #32 on: April 14, 2012, 04:08:09 PM »
Yup.
Thanks. ;)