Jump to content

Batch file to create a folder based on current date & time

Go to solution Solved by WereCatf,
1 minute ago, Ziondaman said:

yes i want it to do it in E:\Dump

Then it's just:

@echo off
E:
cd E:\Dump

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

HI 
is there a way to make a batch that make a folder every day
and putt all the files in 
so like this

2020/12/10 2 files

2020/13/10 1 file

2020/14/10 5 files

2020/15/10 0 files

and del the olde folder if it has 0 files

i am not good on batch programming so i hope somebody can help me  

Link to post
Share on other sites

im not sure about batch, but in bash you can easily do a
 

mkdir $date


the beginning of the script. Whilte the output from $date can be manipulated in many ways to get the date format you want.

Maybe try googleing for something like "$date in batch" etc.

Remember: If you want me to see your reply - Quote me!

 

If your question has been solved, please mark the thread as solved by choosing the answer that has helped you the most as the correct/solving one.

 

Please also use appropriate titles. Titles should be a short description of your situation to get people interested in looking at your thread. Titles such as "help" or "i have a problem" (etc.) will only lead to you getting less to zero help, cause people won't be able to tell what this thread is about and therefore don't bother looking into it.

Link to post
Share on other sites

7 minutes ago, JayBe said:

im not sure about batch, but in bash you can easily do a


mkdir $date

 

Nope, two mistakes: redirection of the output of a command would be $(date), but since the output contains space-characters, you'd also need quotes, ie. mkdir "$(date)"

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

2 minutes ago, WereCatf said:

Nope, two mistakes: redirection of the output of a command would be $(date), but since the output contains space-characters, you'd also need quotes, ie. mkdir "$(date)"

True, i forgot about that, sorry. Too early for me to have my full brain capacity :D

 

 

Just now, Ziondaman said:

so like this

image.png.05434108d60a9505b7f225702274dc83.png

Im not sure if mkdir exists in batch tho, but give it a try :D

Remember: If you want me to see your reply - Quote me!

 

If your question has been solved, please mark the thread as solved by choosing the answer that has helped you the most as the correct/solving one.

 

Please also use appropriate titles. Titles should be a short description of your situation to get people interested in looking at your thread. Titles such as "help" or "i have a problem" (etc.) will only lead to you getting less to zero help, cause people won't be able to tell what this thread is about and therefore don't bother looking into it.

Link to post
Share on other sites

4 minutes ago, WereCatf said:

Nope, two mistakes: redirection of the output of a command would be $(date), but since the output contains space-characters, you'd also need quotes, ie. mkdir "$(date)"

 

14 minutes ago, JayBe said:

im not sure about batch, but in bash you can easily do a
 


mkdir $date


the beginning of the script. Whilte the output from $date can be manipulated in many ways to get the date format you want.

Maybe try googleing for something like "$date in batch" etc.

note 
if there is any other skrip that can do it better
or if there is a sofwere i am willing to chang

i have a log sever but it dumps the longs in random code names like 2kfjjjf or iiidaj
so i need a sorter by folder "days" aka that it makes folders daly and adds the files in there
and del folders that has no files in


 

Link to post
Share on other sites

Remember: If you want me to see your reply - Quote me!

 

If your question has been solved, please mark the thread as solved by choosing the answer that has helped you the most as the correct/solving one.

 

Please also use appropriate titles. Titles should be a short description of your situation to get people interested in looking at your thread. Titles such as "help" or "i have a problem" (etc.) will only lead to you getting less to zero help, cause people won't be able to tell what this thread is about and therefore don't bother looking into it.

Link to post
Share on other sites

13 minutes ago, Ziondaman said:

so like this

image.png.05434108d60a9505b7f225702274dc83.png

No, you did not mention that you are using Windows and you wanna make a .BAT-script. For a Windows batch-file, you'd just use mkdir "%DATE%" or mkdir "%datestamp%" -- whichever variable you want to use. The quotes are still important there, though.

 

The "$(date)" - style is for Bash-scripts and similar Bourne-compatible shells, not the Windows command-interpreter.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Just now, WereCatf said:

No, you did not mention that you are using Windows and you wanna make a .BAT-script. For a Windows batch-file, you'd just use mkdir "%DATE%" or mkdir "%datestamp%" -- whichever variable you want to use. The quotes are still important there, though.

 

The "$(date)" - style is for Bash-scripts and similar Bourne-compatible shells, not the Windows command-interpreter.

 

7 minutes ago, JayBe said:

ok i fix it

 image.png.fbc4fe9bb78c4a08051caa656b6d8876.png
but now how do i make it to move all the files in the new folder
and how do i make it send it so a folder image.thumb.png.eb4ae44efbb54028cd28364af224a7c9.png

the code
 

Link to post
Share on other sites

2 minutes ago, Ziondaman said:

but now how do i make it to move all the files in the new folder
and how do i make it send it so a folder

What files and from where?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

35 minutes ago, Ziondaman said:

vido pdf ext all typs
from a folder 

Since you're not saying what folder, I assume you just mean the folder where the batch-script itself is:

@echo off
REM Set current working-directory to where the bat-file is.
SET mypath=%~dp0
cd "%mypath:~0,-1%"

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

40 minutes ago, WereCatf said:

Since you're not saying what folder, I assume you just mean the folder where the batch-script itself is:


@echo off
REM Set current working-directory to where the bat-file is.
SET mypath=%~dp0
cd "%mypath:~0,-1%"

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

o no now i get it E:\Dump

sorry thx

 

Link to post
Share on other sites

2 hours ago, WereCatf said:

Since you're not saying what folder, I assume you just mean the folder where the batch-script itself is:


@echo off
REM Set current working-directory to where the bat-file is.
SET mypath=%~dp0
cd "%mypath:~0,-1%"

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

now can i set this up and make it run on Task Scheduler
so i fix the folder or do i have to do some more config to make it run??

Link to post
Share on other sites

2 minutes ago, Ziondaman said:

now can i set this up and make it run on Task Scheduler
so i fix the folder or do i have to do some more config to make it run

It depends. Do you want it to create those datestamped folders in E:\Dump, or do you want them somewhere else and move the files from E:\Dump there, instead?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

5 minutes ago, WereCatf said:

It depends. Do you want it to create those datestamped folders in E:\Dump, or do you want them somewhere else and move the files from E:\Dump there, instead?

yes i want it to do it in E:\Dump

Link to post
Share on other sites

1 minute ago, Ziondaman said:

yes i want it to do it in E:\Dump

Then it's just:

@echo off
E:
cd E:\Dump

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Just now, WereCatf said:

Then it's just:


@echo off
E:
cd E:\Dump

REM Create directory
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set foldername=%year%.%month%.%day%
mkdir "%foldername%"

REM Move all files, except the batch-file itself
for %%i in (*) do if not "%%i" == "%~nx0" move "%%i" "%foldername%\%%i"

 

thx man you help me very much

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×