Solved

Move old files to subfolder

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…

751 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