Solved

Move old files to subfolder

Posted on 2013-11-21
1
716 Views
Last Modified: 2013-11-21
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
Comment
Question by:joachimcarrein
1 Comment
 
LVL 4

Accepted Solution

by:
joachimcarrein earned 0 total points
ID: 39665408
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now