Append to a filename last modified date and time with a batch file
I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. I found a script to add current timestamp to it, but I need to add modified date of file. You can see it below:
@echo off
set Source=C:csvtest
set Target=C:csvtestcsvtestArchive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%*.csv") DO (
COPY "%%i" "%Target%%%~Ni %All%.csv")
Thanks in advance for your help.
batch-file batch-rename datemodified
add a comment |
I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. I found a script to add current timestamp to it, but I need to add modified date of file. You can see it below:
@echo off
set Source=C:csvtest
set Target=C:csvtestcsvtestArchive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%*.csv") DO (
COPY "%%i" "%Target%%%~Ni %All%.csv")
Thanks in advance for your help.
batch-file batch-rename datemodified
add a comment |
I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. I found a script to add current timestamp to it, but I need to add modified date of file. You can see it below:
@echo off
set Source=C:csvtest
set Target=C:csvtestcsvtestArchive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%*.csv") DO (
COPY "%%i" "%Target%%%~Ni %All%.csv")
Thanks in advance for your help.
batch-file batch-rename datemodified
I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. I found a script to add current timestamp to it, but I need to add modified date of file. You can see it below:
@echo off
set Source=C:csvtest
set Target=C:csvtestcsvtestArchive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%*.csv") DO (
COPY "%%i" "%Target%%%~Ni %All%.csv")
Thanks in advance for your help.
batch-file batch-rename datemodified
batch-file batch-rename datemodified
edited Nov 23 '18 at 17:33
double-beep
1,7252724
1,7252724
asked Nov 23 '18 at 17:05
chiruchiru
314
314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:Test20181123SO_53450598.cmd
@echo off
set "Source=C:test"
set "Target=C:testArchive"
PowerShell -Nop -C "Get-ChildItem '%Source%*.csv'|Copy-Item -Destination {'%Target%{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
add a comment |
@LotPings way lasts a lot of time if PowerShell isn't loaded.
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down:
for /f ...
is well known to you I believe.
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command (/C
) for file specified in /M
option (file.ext
). "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. @fdate
is the last modified date of the file and @ftime
is the last modified time of the file.
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them.
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
.
For more information type for /? & forfiles /?
in a fresh cmd.
Hope this helps you, as it is faster.
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450598%2fappend-to-a-filename-last-modified-date-and-time-with-a-batch-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:Test20181123SO_53450598.cmd
@echo off
set "Source=C:test"
set "Target=C:testArchive"
PowerShell -Nop -C "Get-ChildItem '%Source%*.csv'|Copy-Item -Destination {'%Target%{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
add a comment |
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:Test20181123SO_53450598.cmd
@echo off
set "Source=C:test"
set "Target=C:testArchive"
PowerShell -Nop -C "Get-ChildItem '%Source%*.csv'|Copy-Item -Destination {'%Target%{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
add a comment |
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:Test20181123SO_53450598.cmd
@echo off
set "Source=C:test"
set "Target=C:testArchive"
PowerShell -Nop -C "Get-ChildItem '%Source%*.csv'|Copy-Item -Destination {'%Target%{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:Test20181123SO_53450598.cmd
@echo off
set "Source=C:test"
set "Target=C:testArchive"
PowerShell -Nop -C "Get-ChildItem '%Source%*.csv'|Copy-Item -Destination {'%Target%{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.
answered Nov 23 '18 at 18:59
LotPingsLotPings
18.4k61532
18.4k61532
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
add a comment |
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
2
2
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
IT works. Thanks a lot for your help.
– chiru
Nov 23 '18 at 19:55
add a comment |
@LotPings way lasts a lot of time if PowerShell isn't loaded.
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down:
for /f ...
is well known to you I believe.
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command (/C
) for file specified in /M
option (file.ext
). "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. @fdate
is the last modified date of the file and @ftime
is the last modified time of the file.
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them.
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
.
For more information type for /? & forfiles /?
in a fresh cmd.
Hope this helps you, as it is faster.
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
add a comment |
@LotPings way lasts a lot of time if PowerShell isn't loaded.
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down:
for /f ...
is well known to you I believe.
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command (/C
) for file specified in /M
option (file.ext
). "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. @fdate
is the last modified date of the file and @ftime
is the last modified time of the file.
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them.
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
.
For more information type for /? & forfiles /?
in a fresh cmd.
Hope this helps you, as it is faster.
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
add a comment |
@LotPings way lasts a lot of time if PowerShell isn't loaded.
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down:
for /f ...
is well known to you I believe.
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command (/C
) for file specified in /M
option (file.ext
). "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. @fdate
is the last modified date of the file and @ftime
is the last modified time of the file.
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them.
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
.
For more information type for /? & forfiles /?
in a fresh cmd.
Hope this helps you, as it is faster.
@LotPings way lasts a lot of time if PowerShell isn't loaded.
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down:
for /f ...
is well known to you I believe.
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command (/C
) for file specified in /M
option (file.ext
). "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. @fdate
is the last modified date of the file and @ftime
is the last modified time of the file.
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them.
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
.
For more information type for /? & forfiles /?
in a fresh cmd.
Hope this helps you, as it is faster.
edited Nov 24 '18 at 19:39
answered Nov 24 '18 at 11:29
double-beepdouble-beep
1,7252724
1,7252724
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
add a comment |
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
With copy operations involved I don't think my batch is remarkably slower, if at all. And OP wants to copy - not rename.
– LotPings
Jan 3 at 19:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450598%2fappend-to-a-filename-last-modified-date-and-time-with-a-batch-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown