Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move old files to subfolder

Posted on 2013-11-21
1
Medium Priority
?
736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

671 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