• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

BAtch File

I have this Batch file that is supposed to copy files from a local hard drive to a network server, but I keep getting either an error that “) was unexpected at this time” or “The syntax of the command is incorrect”.  The error seems to be coming from the second set of FOR loops, but I can figure out why.

@ECHO OFF
:: Check Windows version and command line arguments
IF NOT "%OS%"=="Windows_NT"  GOTO Syntax
IF NOT "%~1"==""             GOTO Syntax
::DISKPART.EXE /? >NUL 2>&1 || GOTO Syntax
:: Localize variable and enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
SET CDACT=
SET Multi=
SET ProjectID=
SET ProjectID2=
SET Item=
FOR /F "tokens=3" %%A IN ('ECHO list volume ^| DISKPART.EXE ^| FIND.EXE "FOR_MEDIA"') DO (
      IF DEFINED CDROM (
            SET CDACT=!CDACT! %%A
            SET ProjectID=!ProjectID! %%A
            SET Multi=s
)       ELSE (
            SET CDACT=%%A
      )
)
ECHO CDACT=%CDACT%
:: Define the PROJECTID
IF DEFINED CDACT (
      for /f "tokens=*" %%B IN ('dir "%CDACT%:" /A:D /A:-H /O:D /B') DO (
            SET ProjectID=%%B
      )
ECHO ProjectID=%ProjectID%
      for /f "tokens=*" %%C IN ('dir "%CDACT%:\%ProjectID%" /A:D /A:-H /O:D /B') DO (
            SET Item="%%C:~-3%"
::Copy Files Over
            IF DEFINED ProjectID (
::Create XCopy Commands Below
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.txt" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.pdf" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.html" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.rtf" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.jpg" "x:\%ProjectID%\photos\%Item%" /V /l /Q /C
::            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.E*" "v:\%ProjectID%\" /V /l /Q /C
            ) ELSE       (
                  ECHO %ProjectID% not found
                  )      
      )
) ELSE (
      ECHO Forensicon Media Not Found!
)

ECHO ProjectID_test=%ProjectID%
ECHO Item=%Item%
:: Done
ENDLOCAL & SET CDACT=%CDACT%
GOTO:EOF
:: Done
IF "%OS%"=="Windows_NT" GOTO:EOF
0
Yaniv Schiff
Asked:
Yaniv Schiff
  • 7
  • 4
  • 3
  • +1
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Replace all "::" with "REM" - "::" is NOT a valid comment marker.  It works like one in 99% of cases, but using it inappropriately can result in problems.  So that's the first thing I would change.
0
 
Neil RussellTechnical Development LeadCommented:
This line:
FOR /F "tokens=3" %%A IN ('ECHO list volume ^| DISKPART.EXE ^| FIND.EXE "FOR_MEDIA"') DO (

You have a single quote before the ECHO , where is the close quote?
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
Thanks for that, here is what the code looks like now:
@ECHO OFF
REM Check Windows version and command line arguments
IF NOT "%OS%"=="Windows_NT"  GOTO Syntax
IF NOT "%~1"==""             GOTO Syntax
REM DISKPART.EXE /? >NUL 2>&1 || GOTO Syntax
REM Localize variable and enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
SET CDACT=
SET Multi=
SET ProjectID=
SET ProjectID2=
SET Item=
FOR /F "tokens=3" %%A IN ('ECHO list volume ^| DISKPART.EXE ^| FIND.EXE "FOR_MEDIA"') DO (
      IF DEFINED CDROM (
            SET CDACT=!CDACT! %%A
            SET ProjectID=!ProjectID! %%A
            SET Multi=s
)       ELSE (
            SET CDACT=%%A
      )
)
ECHO CDACT=%CDACT%
REM Define the PROJECTID
IF DEFINED CDACT (
      FOR /f "tokens=*" %%B IN ('dir "%CDACT%:" /A:D /A:-H /O:D /B') DO (
            SET ProjectID=%%B
      FOR /f "tokens=*" %%C IN ('dir "%CDACT%:\%ProjectID%" /A:D /A:-H /O:D /B') DO (
            SET Item="%%C:~-3%"
REM Copy Files Over
            IF DEFINED ProjectID (
REM Create XCopy Commands Below
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.txt" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.pdf" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.html" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.rtf" "x:\%ProjectID%\Logs" /V /l /Q /C
            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.jpg" "x:\%ProjectID%\photos\%Item%" /V /l /Q /C
REM            xcopy "%CDACT%:\%ProjectID%\Item%Item%\*.E*" "v:\%ProjectID%\" /V /l /Q /C
            ) ELSE       (
                  ECHO %ProjectID% not found
                  )      
      )
      )
) ELSE (
      ECHO Forensicon Media Not Found!
)

ECHO ProjectID_test=%ProjectID%
ECHO Item=%Item%
REM Done
ENDLOCAL & SET CDACT=%CDACT%
GOTO:EOF
REM Done

However here are my results:
CDACT=F
File not found - *.txt
0 File(s)
File not found - *.pdf
0 File(s)
File not found - *.html
0 File(s)
File not found - *.rtf
0 File(s)
File not found - *.jpg
0 File(s)
ProjectID_test=FORENSICON_INTERNAL
Item="FORENSICON_INTERNAL:~-3"

The pathing on the local drive looks like this "F:\FORENSICON_INTERNAL\Item001" where item00x there are actually 3 seperate folders and the code should be running the xcopy command on each of the item00x folders.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Yaniv SchiffDirector of Digital Forensics Author Commented:
One more thing, I cant seem to get the code to assign the correct var to the SET Item="%%C:~-3%" command.  That var should be set to something that looks like "001 or 002 or 003"
0
 
Neil RussellTechnical Development LeadCommented:
Line 13 'ECHO
 
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
FOR /F "tokens=3" %%A IN ('ECHO list volume ^| DISKPART.EXE ^| FIND.EXE "FOR_MEDIA"') DO (

The end Quote is at the end of the line, right next to the Quote after MEDIA"
0
 
Neil RussellTechnical Development LeadCommented:
im going blind, your right!
0
 
erobbyCommented:
First thing is to turn echo on so that you can see where it is exactly failing.  Also insert "pause" after each line.  That way when you get the error you will see it right away and you can identify which command is failing.

Also try setting %%c to something else and use that variable.

After that I'm sure you will be able to see the error
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
After trying a few things my errors have gone away but the xcopy commands are still not coyying any data.  The problem looks like it is coming from the for loop:
      FOR /f "tokens=*" %%C IN ('dir "%CDACT%:\%ProjectID%" /A:D /A:-H /O:D /B') DO (
            SET Item=%%C
Where the Item var should be getting assigned something like Item001, but no matter what I do to the loop %%C I either get Item assigned to either the same thing as projected or nothing at all which then breaks the xcopy commands.  Any ideas?
Thanks
0
 
erobbyCommented:
Is that a typo "%CDACT:%"?  The colon ":" indicates a drive and not a directory
0
 
erobbyCommented:
OOPs sorry defining the drive letter for the CD drive.

 SET "Item"="%%C"

xcopy "%CDACT%:\%ProjectID%\Item%Item:-3%\*.txt" "x:\%ProjectID%\Logs" /V /l /Q /C

Try changing the following lines.

The quotes treats the entire variable as a string to include spaces
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
No, the first for loop in the code is running the disk part command in order to automatically pull the drive letter of my local hard drive.  But that var isn’t being set with colon that is required when changing drive letters in dos, so I had to put the colon in there.
0
 
erobbyCommented:
Can you show an example to the target file path and file name?
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
Here are some screen shots, but the target is currently empty since nothing will copy to that network directory yet.
Original-Paths.docx
0
 
Yaniv SchiffDirector of Digital Forensics Author Commented:
We figured out the code.  the problem was the for loop and pulling the correct token.   Here is the correct code:
FOR /f "tokens=*" %%C IN ('dir "%CDACT%:\%%B" /A:D /A:-H /O:D /B') DO (
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now