0

Is there a way to do this? I have the following:

@echo off
setlocal
for /d /r "someDirectoryPath" %%D in (.) do (
  cd "%%D"
  copy *+
)

However, it does not get files in subfolders of the main directory; it only gets files in the same directory as the script. For example, if I have:

someDirectoryPath 
│
└───folder1
│   │   file011.txt
│   │   file012.txt
│   │
│   └───subfolder1
│       │   file111.txt
│       │   file112.txt
│       │   ...
│   
└───folder2
    │   file021.txt
    │   file022.txt

I would want the batch script inside the main directory, and every file/folder inside that would be updated. Is there a simple way to do this?

Compo
  • 34,184
  • 4
  • 22
  • 36
G27nS4gD
  • 1
  • 2
  • 1
    There can be used `for /F "delims=" %%I in ('dir "C:\some\Directory\Path\*" /A-D-L /B /S 2^>nul') do COPY /B "%%I"+,, "%%I" >nul` as described by [What is Windows equivalent of Touch command to modify timestamp of files in some directory](https://stackoverflow.com/questions/65821025/) or [Touch file to update the date/time of file?](https://stackoverflow.com/questions/6432089/) But it would be better to use PowerShell for this task as described by [Is there a way to use the equivalent of touch in PowerShell to update time stamps of a file?](https://stackoverflow.com/questions/58685459/) – Mofi May 30 '22 at 15:42
  • 1
    See also on ShellHacks the page [Windows: `Touch` Command – Equivalent](https://www.shellhacks.com/windows-touch-command-equivalent/). – Mofi May 30 '22 at 15:43
  • Nevermind, I got it to work when removing the * in the directory name. However, this only updates the files, and not the directories containing the files. Is it also possible to update the folders themselves so that the folder dates are updated as well? – G27nS4gD May 30 '22 at 15:58
  • The files inside the folders did updated, but the dates on the folders themselves did not update. – G27nS4gD May 30 '22 at 16:02
  • There is PowerShell required to update the last modification, creation or last access timestamp of a directory or a program coded in C or C++ using [SetFileTime function](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime) or C# using [File.SetLastWriteTime](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.setlastwritetime). There is no hack like this one for files to change the timestamp of directories. – Mofi May 30 '22 at 16:07
  • There isn't a way to copy all of the files inside a folder in a batch script? – G27nS4gD May 30 '22 at 16:10
  • Also, this just changes the last modified timestamp and not the created timestamp, how do I fix this? – G27nS4gD May 30 '22 at 16:11
  • *Total Commander* is a shareware Windows GUI file manager which can change the last modification time of all files and directories of a directory tree and the user does not need to have any coding skills. It can be done with a view clicks: menu __Commands__, menu item __Search__, button __Start Search__, button __Feed to listbox__, __Ctrl+A__ to select all directories and files, menu __Files__, menu item __Change attributes__, check option __Change date/time__, enter date and time wanted by you, click on __OK__, task done. – Mofi May 30 '22 at 16:12
  • This should be the last question, and I'll edit my posts/delete the others. Do you mind sharing a sample of the robocopy script? – G27nS4gD May 30 '22 at 16:21
  • The creation timestamp of a file is set when the file is created in the __current directory__. Every time a file is copied from one directory to another directory, the creation timestamp is set to current date/time for the file created in the destination directory while the last modification timestamp is kept as is. Why is it important for you that the creation timestamp is changed too? However, the solution is using PowerShell. Please read the referenced pages and do not ask again and again rapidly without giving people willing to help you the time to write comments and answers. – Mofi May 30 '22 at 16:21
  • Use the __Edit__ link of the question and write in the question all requirements you have and explain what you already have. And use the __Delete__ link on the other posts. It is of course possible to use __ROBOCOPY__ to copy the entire directory tree to a different directory on same drive , then used the command __RD__ to delete the original directory tree and use command __MOVE__ to move the newly created directory back to original directory which in real just updates the file allocation table in file system instead of moving all files and folders. Run each command with `/?` for help. – Mofi May 30 '22 at 16:21
  • The second line after `@echo off` is `%SystemRoot%\System32\robocopy.exe "C:\Temp\Test" "C:\Temp\CopyTest" /COPYALL /E /NDL /NFL /NJH /NJS /R:3 /W:5 >nul` to copy entire directory `C:\Temp\Test` to `C:\Temp\CopyTest`. The destination directory is automatically created. `/COPY:DASOU` (copy all except timestamp) is ignored unfortunately by __ROBOCOPY__ and results in using `/COPYALL`. The third line is `if errorlevel 2 rd /Q /S "C:\Temp\CopyTest" & exit /B 1` to delete the copy if something failed and exit the batch file processing. – Mofi May 30 '22 at 16:51
  • The fourth line is `for /F "delims=" %%I in ('dir "C:\Temp\CopyTest\*" /A-D-L /B /S 2^>nul') do COPY /B "%%I"+,, "%%I" >nul` to update the last modification timestamps of all copied files in destination directory. The last line is `rd /Q /S "C:\Temp\Test" && move "C:\Temp\CopyTest" "C:\Temp\Test" >nul` to delete the source directory and (rename) the destination directory to source directory. There can be used here in this special case also `ren "C:\Temp\CopyTest" "Test"` as source and destination directory are in same directory instead of `move "C:\Temp\CopyTest" "C:\Temp\Test" >nul`. – Mofi May 30 '22 at 16:54
  • I don't care about the creation date of each file. The creation date of the directories is definitely set to current date/time as each directory is created new and the __ROBOCOPY__ option `/DCOPY:T` was not used to copy the timestamps of the directories too. – Mofi May 30 '22 at 16:55

0 Answers0