Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

Move old files to subfolder

Hi,

I created a little batch script that will check a specific folder. This folder contains subfolders per customer, and in this subfolders there are files.

First it will create another subfolder underneath the customer folder named old.
Next, it should loop each customer folder and move the files older than 1 day to the old subfolder.

My script runs quite fine, but i'm unable to get my output correct.

if I put my forfiles command to "cmd /c Move XYZ | echo @path" I get an error saying I tried to write to an unexisting pipe. If I move the echo before the "move", the files are placed, but I get no output.

As a second problem, I also get a message stating there are no result for the forfiles function, which I can hide with 2>NUL, but I am unsure wether or not I would lose the result of the echo @path as well.

If I was able to get the output correct, it would be nice to only show the echo %%m for the folders where there actually are files, but that's just a nice to have :)

here is my script:
@echo off
ECHO.
for /D %%m in (*) DO md "%%m"\old 1>NUL 2>&1
ECHO.
for /D %%m in (*) DO forfiles -P "%%m" -D -1 -M *.* -C "cmd /c move /Y @path old/" | echo %%m: 

pause

Open in new window


oh, btw, if this is easier to accomplish using powershell, i'm open to suggestions, i'm just not into powershell yet :)
0
Joachim Carrein
Asked:
Joachim Carrein
1 Solution
 
Joachim CarreinSr. Software DeveloperAuthor Commented:
I think I finally found it:

@echo off

for /D %%m in (*) DO call :process %%m
ECHO.
ECHO DONE
ECHO.
pause
goto :eof

:process
forfiles -P "%1" -D -1 -M *.* 1>NUL 2>&1
IF %ERRORLEVEL%==0 (
  echo %1
  md "%1"\old 1>NUL 2>&1
  forfiles -P "%1" -D -1 -M *.* -C "cmd /c echo   @path" 2>NUL 
  forfiles -P "%1" -D -1 -M *.* -C "cmd /c move /Y @path old/ 1>NUL"
)

:eof

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now