Solved

Move old files to subfolder

Posted on 2013-11-21
1
726 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

10 Experts available now in Live!

Get 1:1 Help Now