need batch help again this time almost complete script

Discussion in 'Software' started by red death68, Jan 7, 2010.

  1. red death68

    red death68 Command Sergeant Major

    since i know germanone will probably respond to this he might know the code better but anyways what i want to do is to make a batch on my flash drive ask me if i want to delete a pre-existing folder of my desktop and completly replace it or just copy the new files into it heres what i have so far

    @echo off &setlocal
    set /p "answer=Do you want to copy? [y/n] "
    if /i "%answer%" neq "y" goto :eof
    if exist "%userprofile%\Desktop\crossfire" rd /s /q "%userprofile%\Desktop\crossfire review and guide"
    md "%userprofile%\Desktop\crossfire review and guide"
    xcopy "%~d0\\crossfire review and guide" "%userprofile%\Desktop\crossfire review and guide" /c /q /i /e /y

    tyvm to anyone who helps (*cough*germanone*cough*)
     
  2. SWario

    SWario Sergeant

    What does "%~d0" represent? The flash drive? If you are running this from the flash drive, you only need to specify the relative path, so "crossfire review and guide" is fine, if both are stored in the root directory.

    Other than that, so far you check for one folder, but delete another. Why?

    I think that the below code does what you want:
    Code:
    @echo off &setlocal
    
    REM Prompt user for choice
    set /p "answer=Do you want to copy? [y/n] "
    
    REM if answer is not equal to "y" or "Y", go to "end of file" (END)
    if /i "%answer%" neq "y" goto :eof
    
    
    :choice
    REM check for full rewrite, or just update
    set /p "answer=Do you want to Rewrite all files, or just Update files? [r/u] "
    if /i "%answer%" == "r" goto :rewrite
    if /i "%answer%" == "u" goto :update
    echo You have selected an invalid option.
    goto :choice
    
    
    :rewrite
    REM remove existing directory and copy all files fresh from source
    REM if "Desktop\crossfire" exists, remove "Desktop\crossfire review and guide" completely and quietly
    if exist "%userprofile%\Desktop\crossfire review and guide" rd /s /q "%userprofile%\Desktop\crossfire review and guide"
    
    REM remake the directory
    md "%userprofile%\Desktop\crossfire review and guide"
    
    REM xcopy "crossfire review and guide"; continue through errors, don't display filenames,
    REM  if the dest doesn't exist and copying more than one file assume the dest is a directory,
    REM  copy directories and sub-directories including empty ones, overwrite automatically
    xcopy "crossfire review and guide" "%userprofile%\Desktop\crossfire review and guide" /c /q /i /e /y
    
    goto :done
    
    
    :update
    REM only copies files whose source date is newer than the destination date
    REM  continue through errors, don't display filenames,
    REM  if the dest doesn't exist and copying more than one file assume the dest is a directory,
    REM  copy directories and sub-directories including empty ones, overwrite automatically
    xcopy "crossfire review and guide" "%userprofile%\Desktop\crossfire review and guide" /c /q /i /e /y /d
    
    
    :done
    echo Complete!
    pause
    
    Of course, review and modify to your needs, and let me know if I misunderstood something.
     
  3. GermanOne

    GermanOne Guest

    Hi red death68.

    SWario's code looks absolutely fine for me and his explanations are excellent.
    He's also right with your line:
    Code:
    if exist "%userprofile%\Desktop\[highlight]crossfire[/highlight]" rd /s /q "%userprofile%\Desktop\[highlight]crossfire review and guide[/highlight]"
    
    Suchlike will not work.

    If you want to use an absolute path, why do you write double backslashes?
    Code:
    xcopy "%~d0[highlight]\\[/highlight]crossfire review and guide" "%userprofile%\Desktop\crossfire review and guide" /c /q /i /e /y
    
    One is enough.

    BTW: I think it's all just a careless mistake. Read your batch codes two times more - it's easy to find, isn't it?
    A good way to debug a batch code is to replace "@echo off" with "echo on" and write some PAUSE commands to your code to figure out where you are. E.g.
    Code:
    echo *** bookmark #1 ***&pause>nul
    
    regards
    GermanOne
     
  4. red death68

    red death68 Command Sergeant Major

    k ty german and the double back slashes was prob when i changed folder names i had a teacher breathing down my neck so i overlooked it

    and i will use swario's code and let you guys know the result

    EDIT:k just ran it took a few because of the 200+files that were there but it works perfect ty vm guys. maybe in the future ill learn batch enough i wont have to come here for every batch file i wanna make
     
  5. SWario

    SWario Sergeant

    Glad I could help, red death! Thanks for the compliment, GermanOne!

    Indeed, using echo on can help you see at what lines you're having trouble. Using pause can also give you "checkpoints" to figure out where in the program you are.

    Good luck!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds