Page 2 of 3

Re: Move/sort files to subfolders based on the file's date?

Posted: 18 Aug 2012 23:49
by Liviu
Ed Dyreen wrote:But there are still some ANSI characters that just can't be handled in DOS.

Don't sell your code short ;-) No pipes, no temp files, it should be immune to codepage/unicode issues.

Liviu

Re: Move/sort files to subfolders based on the file's date?

Posted: 19 Aug 2012 00:02
by foxidrive
Ed Dyreen wrote: ) do 2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a" &move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"


For this command:

2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a"

won't this suffice? MD can create trees now...

2>nul md "%%~c\%%~b\%%~a"

Re: Move/sort files to subfolders based on the file's date?

Posted: 19 Aug 2012 02:08
by ©opy[it]®ight
foxidrive wrote:
©opy[it]®ight wrote:Before i make another attempt to try to sleep, i have a final 'challenge' for you Ed Dyreen.


This is not a place to challenge people to write batch files for you. It makes you seem like a prat who uses other people for your own gain.

Make the script so that it doesn't try to create the same folder over and over, but add an IF EXIST statement (like abc0502 did with his script).
Then it will skip the MD command if it encounters an already existing folder ;-)


Why?? The overhead of the command and redirecting to nul is small.

And why can't you add an if exist command yourself if you want it?


I understand you concerns, but i basically have a very shallow knowledge of the commands that are used.
If i knew how to do this myself, i wouldn't have posted here. Simply because i wouldn't need this resource.

I've already been on sites such as SS64, stackoverflow, robvanderwoude etc. searching for the answers for the problems i was facing,
but instead of those websites you people solved it/assisted me, and i greatfully thank you for that!

If i could think of anything to contribute to the forum, i would've done it, unfortunately your level of understanding scripts far exceeds mine.

I'm sorry.

Re: Move/sort files to subfolders based on the file's date?

Posted: 19 Aug 2012 02:13
by foxidrive
okedoke.

This line should work.

Code: Select all

) do (if not exist "%%~c\%%~b\%%~a\"  md "%%~c\%%~b\%%~a") & move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"


replace this one

) do 2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a" &move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"

Re: Move/sort files to subfolders based on the file's date?

Posted: 23 Aug 2012 11:54
by ©opy[it]®ight
Thanks for the help, everyone (much appreciated)! Long Live DOS/batch :mrgreen:

(i was off for a coupe of days so i couldn't respond earlier)

edit:

I was bored and made some changes to the script, but for some reason my console window often gets flooded with the following error:
the process cannot access the file because it is being used by another process


Here's what the script looks like atm:

Code: Select all

@ECHO OFF

SETLOCAL DisableDelayedExpansion

CLS
TITLE copyitright's stats CoSoLo (Count, Sort, Log) v0.99

SET DATUM=%DATE%
SET TIJD=%TIME%

echo.
ECHO  Stats CoSoLo (Count, Sort, Log) v0.99
ECHO  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
echo.
echo.>>stats.log
ECHO  [%TIJD% @ %DATUM%] ER ZIJN NOG GEEN BESTANDEN VERPLAATST!
ECHO  [%TIJD% @ %DATUM%] ER ZIJN NOG GEEN BESTANDEN VERPLAATST!>>stats.log
goto LOOP

:COUNT
SET DATUM=%DATE%
SET TIJD=%TIME%

IF "%COUNTER%"=="1" (
  ECHO  [%TIJD% @ %DATUM%] ER IS TOT NU TOE 1 BESTAND VERPLAATST!
  ECHO  [%TIJD% @ %DATUM%] ER IS TOT NU TOE 1 BESTAND VERPLAATST!>>stats.log
) ELSE (
  ECHO  [%TIJD% @ %DATUM%] ER ZIJN TOT NU TOE %COUNTER% BESTANDEN VERPLAATST!
  ECHO  [%TIJD% @ %DATUM%] ER ZIJN TOT NU TOE %COUNTER% BESTANDEN VERPLAATST!>>stats.log
)

:LOOP

:: Code Inspired By abc0502 & Ed Dyreen / Modified by foxidrive & copyitright ::

for %%§ in (

       "*.TXT"

) do   set "$date=" &for %%? in (

       %%~t§

) do   if not defined $date set "$date=%%~?" &for /f "tokens=1-3 delims=/-" %%a in (

       "%%~?"

) do   (if not exist "%%~c\%%~b\%%~a\" md "%%~c\%%~b\%%~a") & (

  move /y "%%~§" "%%~c\%%~b\%%~a\%%~§" >NUL

  IF ERRORLEVEL 1 goto LOOP

  SET /A COUNTER+=1
  goto COUNT
)

ping -n 2 127.0.0.1>NUL
goto LOOP


I could choose to redirect these errors to NUL but i'm more into identifying the cause of it, and (whenever possible) work around this problem instead of ignoring/hiding it ;-)

I've already tried to suppress the error by using:

Code: Select all

IF ERRORLEVEL 1 goto LOOP

but it doesn't seem to fix/solve anything.

Regards
- copyitright

Re: Move/sort files to subfolders based on the file's date?

Posted: 23 Aug 2012 12:39
by Ed Dyreen
©opy[it]®ight wrote:my console window often gets flooded with the following error:
the process cannot access the file because it is being used by another process

I think
the process cannot access the file because it is being used by another process
:D

Code: Select all

@echo off

> locked (

       call :exception
)

pause
exit

:exception
:: (
   > locked (

      echo.fails because file is in use.
   )
:: )
exit /b 1

Code: Select all

Het proces heeft geen toegang tot het bestand omdat
het bestand door een ander proces wordt gebruikt.
Druk op een toets om door te gaan. . .
Because the file was opened in a way that doesn't allow concurrent writes, you either find out which process is currently using the file and terminate it.

Code: Select all

openFiles /?
or redirect all errors to nul.

Code: Select all

2>nul (

           echo.stream 1 'stdout' prints fine
       >&2 echo.stream 2 'stderr' won't print because of redirection to nul
)

Re: Move/sort files to subfolders based on the file's date?

Posted: 23 Aug 2012 16:26
by ©opy[it]®ight
Ed Dyreen: thanks for the tip on the OpenFiles command!
Never used it nor knew of its existence, until now :)

This will help me lots ;)

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 08:36
by ©opy[it]®ight
Hello again :) (how's everyone doing)

Hopefully this thread is the right place for my problems. If not: let me know ;-)

Since my initial question about how to move/sort files, based on the file's date, i've been experimenting a lot with FOR loops and tokens.
The code/help abc0502, Ed Dyreen and foxidrive gave were a good starting point for me to play around with it and modify it even further to my needs.

Unfortunately, new problems have arised and i haven't yet been able to tackle/solve them.

The situation is as followed:

I have a script that logs some statistics of a batch program i've made.
Whenever someone uses the program, it uploads a file which has the following format:

ip - name - version of script.txt

Now, at the time i created this topic i was looking for a way to somehow sort the files, based on their date of creation.
Because of some very helpful people, i was (am) able to sort/categorize this.

But.. i want to take it a step further: i would like the sorted files to be moved into a (sub)folder that is based on the IP address that is in the filename.

And because i don't want to come over as a prat (again), i'll share my fail code with you.... :oops:
Oh, the code is far from complete, but its all i have for now:

Code: Select all

@ECHO OFF

SETLOCAL DisableDelayedExpansion

CLS

:LOOP
FOR %%A IN (

  "*.txt"

) DO SET "FULL=%%A" &FOR /F "tokens=1 delims= " %%B in (

  "%%A"

) DO SET SHORT=%%B &(

ECHO %FULL%
ECHO %SHORT%
echo.
pause
)
EXIT


Besides being a noob/newb that tries to achieve something (and fails miserably), i'm trying to get the value FULL and SHORT pupulated with the results of %%A and %%B, so that i can work on a way to move the sorted txt %%A into its (sub)folder that is based on the variable %%B.

Of course, i'm doing it totally wrong and the variables %FULL% and %SHORT% stay empty...

Again, i hope that someone is willing enough to assist/help me (into the right direction).
I've never asked (nor will i) someone to write a complete/full working code/script, i just want to make something work(ing), but simply aren't talented/skilled enough to do it on my own.. :/

Best regards (from The Netherlands)

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 10:20
by abc0502
ip - name - version of script.txt

is the spaces between the ip and - sign exist in the real names?

and about your code you should enable the delayed expansion and use the !FULL! instead of %Full% because you are looping throw many files and the moving command then must be in the for loop not out cause the !! dosn't work out the for loop unless you set it first


EDIT


I assumed the spaces exist, if not replace any "!IP:~0,-1! with "%%a" and remove that line

Code: Select all

set "IP=%%a"


Now this is the code

Code: Select all

@echo off & cls

pushd %userprofile%\Desktop\New Folder
Setlocal EnableDelayedExpansion
For /R %%z in (*.txt) Do (
   For /F "tokens=1,2,3 delims=-" %%a in ("%%~nz") Do (
   set "IP=%%a"
      IF not exist "!IP:~0,-1!\" MD "!IP:~0,-1!"
      IF exist "%%a-%%b-%%c.txt" ( Move /Y "%%a-%%b-%%c.txt" "!IP:~0,-1!" >nul )
   )
)

pause

The First For command go throw all txt files and assign it's "FULL" location to "%%z", and in the second For
Command, we use only the file name which will be the "%%~nz" just the file name.

Now in the second For command we will slice the file name into 3 parts the delims will be the " - " sign
and the first part "IP" will have a space in the end so we assign it to variable and then remove the space by using
this "!IP:~0,-1!"
and create the folder if they not exist, then move all files that has the same "IP" part to it's
folders that hold the same "IP" part.

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 18:02
by ©opy[it]®ight
Hi abc0502! :)

I appreciate it that you take it up for me! I was literally becoming insane after having tried so many variations that didn't work out for me.

Your code does exactly what i was looking for (i did tweak your code a bit, by using a space for delimiter and "!IP!" instead of "!IP:~0,-1!")

Code: Select all

@echo off & cls

Setlocal EnableDelayedExpansion

For /R %%z in (*.txt) Do (
   For /F "tokens=1,2,3 delims= " %%a in ("%%~nz") Do (
   set "IP=%%a"
      IF not exist "!IP!" MD "!IP!"
      IF exist "%%z" ( Move /Y "%%z" "!IP!" >nul )
   )
)

pause


All files are in their appropiate folder but there are still way to many of them, to make it manageable.

This is where my initial question came in play (the main reason i created this topic), where i wanted to move the files to a year/month/day subfolder that was based on the file's creation date (i'm sure you still remember ;) ).

And yes, i confess that i'm afraid to ask this:

Even though i have all the code that is needed to (1): sort/move the files to a subfolder that is based on the file's date of creation,
and (2): take it a step further and sort/move the file to a subfolder that is based on the IP address that is found in the filename,
i still don't know how to merge those pieces together into one (ummm, can't find the word for it)... into one, part? (meh..).

I know i'm good at writing long posts, but i'm afraid i still can't contribute to this forum, when there are so many higher skilled people around.

Of course i can show the script i'm working on, but its around 22kb and most of it is not in scope of this topic's subject...

So, what i basically want is.... (to sum it all up):

1. look at the file's date of creation
2. extract the ip address that is found first, in the filename.
3. sort/move the file to a subfolder that is based on the following format:

year\month\day\ip address\file.txt

(thanks for reading all this TLDR-stuff)

Best regards,
a noob/newb (not a prat)

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 18:48
by foxidrive
This expects a date format similar to "Tue 04/09/2012 time:sec"

Test it on a couple of files and copy some of the lines that appear on your screen if your format is different, as the tokens will need to be altered.

Just a tip - *never* ask for just part of a task. Always ask for the complete task and give the details, as the code for a complete task often bears no resemblance to the code for a simpler task, even if it is similar. Batch files depend on the complete information.


Code: Select all

@echo off
       for %%a in (*.TXT) do (
          echo %%~ta
             for /f "tokens=1-4 delims=/- " %%d in ("%%~ta") do (
               for /f %%z in ("%%a") do   md "%%g\%%f\%%e\%%z" 2>nul & move /y "%%a" "%%g\%%f\%%e\%%z"
             )
        )
pause

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 19:42
by ©opy[it]®ight
Hi foxydrive,

You are right.. i totally forgot about that (sorry abc0502! :oops: )

I had to change the code a bit, as the format i have here is like:

di 04-09-2012 (where di stands for dinsdag = tuesday)

The final result of the script/code:

Code: Select all

@echo off

   for %%a in (*.TXT) do (
      for /f "tokens=1-4 delims=/- " %%d in ("%%~ta") do (
         for /f %%z in ("%%a") do ( if not exist "%%f\%%e\%%d\%%z" md "%%f\%%e\%%d\%%z") & (move /y "%%a" "%%f\%%e\%%d\%%z" >nul
         )
      )
   )

pause


I never expected it to be so.. compact.

Thanks to (the) both of you! (its 3:41 am here, so guess what i'm going to do next)

Night.. ;)

edit: oh, didn't see your post until now, Ed! I'll respond to it as soon as i've catched some sleep (i hope you don't mind ;))

ps: i borrowed the code from foxidrive, so don't blame me for doing something wrong :mrgreen: (good night, for now!)

Re: Move/sort files to subfolders based on the file's date?

Posted: 03 Sep 2012 19:44
by Ed Dyreen
©opy[it]®ight wrote:

Code: Select all

if not exist "%%f\%%e\%%d\%%z" md "%%f\%%e\%%d\%%z"

Are you checking to see whether a file exist, or a directory ? And if a file does exist what will happen when you execute makeDir ?

Code: Select all

if not exist "%%f\%%e\%%d\%%z\" md "%%f\%%e\%%d\%%z"
But there still is a small chance it fail; what if the result of the comparison becomes untrue after the comparison completes ?

Your solution is slow

Code: Select all

if not exist "%%f\%%e\%%d\%%z" md "%%f\%%e\%%d\%%z"

Code: Select all

1 look ahead and
2 compare result and select action a or b
3a try
3b do not try
versus

Code: Select all

2>nul md "%%f\%%e\%%d\%%z"

Code: Select all

1 set up redirection
2 try

Ed

Re: Move/sort files to subfolders based on the file's date?

Posted: 04 Sep 2012 06:28
by ©opy[it]®ight
Good afternoon Ed! (2:15pm)

Ugh.. i totally forgot about that trailing backslash (i knew about it).

btw: if doing cd /? i see that the examples they use start with a backslash.
Are they necessary or can they be left out?

- copyitright

Re: Move/sort files to subfolders based on the file's date?

Posted: 04 Sep 2012 07:02
by Ed Dyreen
©opy[it]®ight wrote:if doing cd /? i see that the examples they use start with a backslash.
Are they necessary or can they be left out?
I haven't seen anyone do that in this thread, but when cd starts with a backslash, that is especially important.

Code: Select all

C:\PROFSYS\ADMIN>cd \

C:\>cd profsys\admin

C:\PROFSYS\ADMIN>cd ..\..\PROFSYS

C:\PROFSYS>cd \admin

C:\ADMIN>