Date and time a file was copied to a directory - windows

Is there a way to see what time a file get copied to a directory? Looks like Date modified column in windows explorer shows the date and time when the file got created.

You could try checking this via the command line.
Here are some commands that may help.
Using 'dir'
This, for example, gives the last modified time of this file:
dir /T:W d:\test.pdf
And this gives the date/time for all files and sub-directories in the current one:
dir /T:W
Using 'forfiles'
Modified datetime for all files in current dir:
forfiles /C "cmd /c echo #file #fdate #ftime"
Or just the pdf files within the directory:
forfiles /M *.pdf /C "cmd /c echo #file #fdate #ftime"
Using 'cp' in linux
I don't know who useful this would be for you, but on Linux machines you can use this option when coping files:
$ cp --no-preserve=timestamps my-file.pdf my-copy.pdf
This way the copy will have its own creation time and won't depend on the original file.

Related

Windows batch - where does this command write?

I used this command in a windows command line:
C:\Users\myuser\Desktop>C:\Windows\System32\ForFiles.exe /P C:\myfolder\mysubfolder /S /M *.* /D +09/15/2022 /C "cmd /C echo #FSIZE >> sizes.txt"
I wanted to echo all the sizes for files in folder modified in the last 5 days.
I didn't found the output file.
I then solved the problem by changing the command to:
C:\Users\myuser\Desktop>C:\Windows\System32\ForFiles.exe /P C:\myfolder\mysubfolder /S /M *.* /D +09/15/2022 /C "cmd /C echo #FSIZE" > sizes.txt
Anyway, I'd like to know if I created a sizes.txt file somewhere on my hard drive.
Searched in the folder, subfolder, desktop, home folder, C:, C:\Windows, C:\Windows\System32... nothing...
I finally found them, yes "them".
One in each directory containing recently edited file(s).
Seems that the command forfiles executes is actually run where the file is located.
It's strange because if you specify cmd /C echo %CD% as command it actually prints the directory you run from, in my case Desktop!

Scheduled BAT - Error during XCOPY if file is in use

I have this script scheduled every hour:
#echo off
set path1="E:\Document\Backup"
set path2="E:\Document\NewDoc"
set path3="C:\ScanDoc"
forfiles -p %path1% -s -m *.pdf /D -30 /C "cmd /c del #path"
xcopy %path2%\*.pdf* %path1% /c
start /d %path3% ScanBatch.exe
Files in "NewDoc" folder are created by manual document scanning (PDF FORMAT), so sometimes documents are in use.
The Scanbatch program read files in "Backup" folder, so if PDF is copied from "NewDoc" to "Backup" while in use, it's result as corrupted and the Scanbatch go in error.
Is there a way to copy files ONLY IF NOT IN USE?
At the end the real problem wasn't xcopy, but "Scanbatch.exe" that crashes if found an opened file. Problem solved changing schedulation time.

how to combine forfiles, copy, and #fname?

This command is supposed to copy multiple files from a static source folder into each folder for a set of saved web pages:
forfiles /m *.htm /c "cmd /c copy /y _core/*.* #fname_files"
However, each call fails with a status of, "The system cannot find the file specified."
If this is tried:
forfiles /m *.htm /c "cmd /c copy /y 0x22_core/*.*0x22 #fname_files"
the status displayed shows the name of each source file and the same error message.
I've also tried adding setlocal/endlocal around the call but it still fails.
Searching on the web brought lots of discussions but nothing showing forfiles, cmd, and copying into a destination directory using #fname.
Would someone with deeper knowledge of batch scripting "fix" this line so it works as intended?
If I understand correctly what you are trying to do :
1)You might be getting "The system cannot find the file specified." because the directory _core is not in the same directory as the *.htm files
2)In order to copy the *.htm files into each #fname_files folder, you must create the folder first. Here is the command line with mkdir added :
forfiles /m *.htm /c "cmd /c mkdir #fname_files & copy /y _core\*.* #fname_files"

LIST (not delete!) directories older than X using command line

I need to get a list of directories older than 90 days from 'today's' date using windows command line so I can schedule a batch file to run these folders through a separate process.
I need to list folders and subfolders only, no files.
I have tried using 'dir' which produces the list I need:
dir /b /s /a:d /o:gen >C:\file_list.txt
But, I can't check for dates greater than X using 'dir'.
I've looked at using 'forfiles' but this won't produce the list as I need it which my dir attempt produces.
To add some more complexity, ideally I need to check the date on the files within a folder rather than the folders date properties.
Any help is greatly appreciated, thanks.
And if you try with Forfiles like that :
#echo off
set OLD=90
FORFILES /S /D -%OLD% /C "cmd /c IF #isdir == TRUE echo #path"
pause
For more information about Forfiles command

Command Line to Delete Files and Folders and Sub Folders in Directory, Except Those Who Were Created Today

I want to write a batch file to cleanup my Downloads folder by deleting everything in it Except Files and Folders that were created today.
Thanks.
If you are using modern Windows, recommend you use forfiles,
Folder can still messy. Do you want to do it based on the timestamp of the directory itself? Do you want to process recursively through all folders, deleting files based on date and then delete the folder if it empty after deleting the files of given age. There are other reasonable interpretations of your question as well. Personally, I use a python script so I can make the file cleanup do exactly what I want. This may also be why Uriil suggested PowerShell.
Arguably, Windows Services for Unix downloadable from Microsoft would be considered fair game (allowing the find command mentioned by Johnride). If you use this, make sure Johnride suggestion matches your actual intent. For the find command, using the option -print instead of -exec is great for debugging
If you can use forfiles, this article may give what you want. I taking the liberty of pasting in the batch file solution using forfiles from the article.
#echo off
:: set folder path
set dump_path=c:\shares\dump
:: set min age of files and folders to delete
set max_days=1
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path"
:: remove sub directories from %dump_path%
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path"

Resources