Search code examples
windowsbatch-filecmdbackup

Batch-file: how to backup all .doc files to a folder


I have this small script that is intended to backup all *.doc files in the folder into a _z subfolder. I have just noticed it does not work properly, when _ is part of the filename of the *.doc that is supposed to be backed up.

Could anyone kindly explain to me the source of the issue and perhaps advice on how to correct it, please?

mkdir _z
copy *.doc _z\*"_""%date:/=-%"" ""%time::=-%".doc

Solution

  • Currently you are telling it to copy everything up to the underscore, if one exists, else it will create one if it does not. That way the text after underscore is ignored. try a for loop

    @echo off
    mkdir "_z">nul 2>&1
    for %%i in (*.doc) do copy "%%~i" "_z\%%~ni_%date:/=-% %time::=-%%%~xi"