make batch file that creates a folder with today's date then moves files from a folder in to that newly created folder

前端 未结 6 995
抹茶落季
抹茶落季 2021-01-06 09:56

I need to make a batch file that will make a folder with today\'s date in month day year format (example 080112). Then once it\'s created i need to move files from a set fol

相关标签:
6条回答
  • 2021-01-06 10:19

    @echo on

    :: Use date /t and time /t from the command line to get the format of your date and :: time; change the substring below as needed.

    :: This will create a timestamp like yyyy-mm-dd-hh-mm-ss. set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%

    @echo TIMESTAMP=%TIMESTAMP%

    :: Create a new directory :: md e:\example\"%1\%TIMESTAMP%" xcopy /y c:\windows E:\windows\%TIMESTAMP% /e

    @echo on

    0 讨论(0)
  • 2021-01-06 10:23

    This will show you how to set the date in variables.

    The rest is just using copy/xcopy to that folder :)

    Tell me if you need more elaboration on how to do it.

    Cheers!

    [EDIT]: Here is the complete solution:

    Create a file using notepad -> save as "something.bat" OR using CMD -> copy con something.bat (and once you're done press Ctrl-Z) And paste the following code:

    @echo off
    IF "%1"=="" GOTO MissingArgument
    for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
    for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
    for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
    set TODAY=%month%%day%%year%
    md %TODAY%
    MOVE %1\*.* %TODAY%
    GOTO end
    :MissingArgument
    echo Incorrect Syntax: Source Folder Name Required!
    :end
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-06 10:29
    FOR /f "tokens=2-4 delims=/ " %%i in ('DATE/T') do SET today_fname=%%i%%j%%k
    cd c:\myfolder\%today_fname%
    REM This creates a folder named 05242016 in c:\myfolder
    
    0 讨论(0)
  • 2021-01-06 10:34

    Just rename the folder with Erik's suggestion:

    move FolderName FolderName_%date:~7,2%%date:~4,2%%date:~12,4%
    
    0 讨论(0)
  • 2021-01-06 10:35
    set TODAY=%date:~10,4%%date:~7,2%%date:~4,2%
    

    is an alternative way to get the date part into a shell variable

    from: http://stevesgeekspeak.com/2010/01/howto-get-variable-substrings-in-batcmd-scripts/

    Jony ... FTW, of course, for having the whole answer.

    0 讨论(0)
  • 2021-01-06 10:36

    Was having trouble with this one myself, but directions without further ado: Put your source folder here after the .bat file:

    yourscript.bat c:\users\myname\Desktop\sourcefolder
    

    Hope that helps someone else, took me a few seconds :D

    0 讨论(0)
提交回复
热议问题