0

There are some vaguely or similar questions around this which I tried to piece together to the best of my ability, but not sure if I did it correctly or not.

The goal of the batch file is to look for the date on the filenames, create a folder in which the month and year of this filename's date specifies with a "01 " prefix, and move it into there. The location of the created folder will be one level above where the current log files will reside in. If a folder name already exist with the prefix "01 " but is of a different month and year, it will create one with a prefix of "02 ", and if that already exist, then it will go on to create one with a prefix of "03 " and this goes on until it doesn't find any more folders with the prefix it's trying to create that's already taken and proceeds to create a month year folder with that unused prefix.

Now the code, this is what I have right now:

SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.log) do (
    if exist *-10-2017*.log (
        if exist 01* && not *01*.log (
            set "iter="
            set /p iter=01
            set /a iter+=1 
            set f1=!%inter%" October"!
            md "..\!f1!"
            move "%%a" "!f1!"
        ) else (
        md "01 October 2017" 2>nul
        move "%%a" "01 October 2017"
        )
    ) 
)

That's just for October 2017, so what that should do is check log filename's date, if it has "-10-2017" anywhere in the filename, create a folder "01 October 2017" in a directory above the current and move there, assuming there doesn't exist another folder called 01 May 2017 for example or any other folder starting with the name 01(except an already created destination folder in which case the batch would just move file over without needing to create a separate folder of the same month and year but with a different prefix which is unnecessary, so I don't want "01 October 2017" and "02 October 2017" folders if there already exist a 01 October 2017, for example), it should be cool and if there is, it'll iterate until it finds an unused number, going +01 each go, and then proceed to use that as the prefix.

But that doesn't work because I am stopped at:

E:\SteamCMD\KFServer\UserLogs\11\unread\test>SETLOCALENABLEDELAYEDEXPANSION
&& was unexpected at this time.

I have already found a working piece which is where I modded this from:

SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.log) do (
    set f=%%a
    set g=!f:~92,10!
    md "!g!" 2>nul
    move "%%a" "!g!"
    )
)

But it just moves them into their respect date folders, where I want them to be in month year naming convention and one level up as well and also to add the 01 prefix onto the folder names for easy sorting and searching for later.....also another problem with that is if another log of the same month and year but different day comes through(for example 15-10-2017.log and 10-10-2017.log), it will be in their own folder creating more clutter when I can just group logs by month year rather than individual days....

So I am left with two choices - throw my arms in the air with this or just use the working batch but manually do the additional tasks to finish it up.....so yes, preferrably I would like to let the computer do it all for me with the least amount of effort from me, so please help me with this or correct my code and explain why it's wrong or that it must be this way....

Kind Regards, New(oo)b Coder

  • Your `if` syntax is wrong (see `if ?`). There is no `&&` or `AND` or anything like that. Also note: numbers starting with `0` are handled as octal - and `08` isn't a valid octal number (besides, that incrementing with leading zeros doesn't work like this) – Stephan Sep 10 '18 at 07:41
  • See, if [this](https://stackoverflow.com/a/50805697/2152082) helps – Stephan Sep 10 '18 at 07:45
  • @Stephan thanks for that; what about checking for and implementing the prefixes for naming the folders? EDIT: Then how do you join statements together if you can't use && or AND...? How do I increment with leading zeros then? – Newb Coder Sep 10 '18 at 09:31
  • @Stephan, also that script took 1 minute and 40 seconds to just sort through 11 test files, after editing the code to reflect the changes needed for my log files to work instead of excel files and to add the bit to go up a level and create the folder(s) there rather than in the same directory level.....and also a space inbetween the month and year words....if I get this going, I'll be looking at letting it run through over 10k log files.......which means a minimum of 11.57407407407407 days to let the computer on straight for..... – Newb Coder Sep 10 '18 at 09:42
  • @Stephan over 100k log files left actually, my mistake, and since I can no longer edit the previous comment as it's been over 5 minutes....so that's 115.7407407407407 days.... I had already manually try to go through about 30k of them before even attempting to decide to automate it via a batch script, only over 100k remains to sort through either manually or automated with batch script.....and oh man was that a chore! Hahahahahaha – Newb Coder Sep 10 '18 at 09:49
  • @Stephan, I smart....it's 10.52188552188552 days minimum with over 100k files, not 115, forgot to divide by 11...... regardless, that's still pretty long..... Ideally would like it be automated in less than an hour, preferably in a few minutes or less........ – Newb Coder Sep 10 '18 at 10:42
  • `if exist a.txt if exist b.txt echo both exist` – Stephan Sep 10 '18 at 12:32
  • Sorry, I still can't work out what that `01` prefix is meant to be. For resulting folders to be easily sorted, wouldn' a name in `YYYY-MM` format be ideal? – Stephan Sep 10 '18 at 15:20
  • @Stephan, No because then April would be at the top of the list when sorting by name(As that's the default sorting in file explorer) and it'd go `2017 April, 2017 August, etc.,` when I want it to start with `January 2017, February 2017`, but it list in alphabetical order and not in calendar month year order, I would need to have leading zero and a digit next to denote order. So `01 January 2017, 02 February 2017, 03 March 2017, etc.,` Oh woops I read wrong, it just so happens I started with `MMMM YYYY` naming scheme and have stuck with that ever since I started manually sorting by eye and hand – Newb Coder Sep 10 '18 at 23:39
  • Forgot to add that I've grown up with the `D(D)-MM(MM)-YY(YY)` convention...so the day is always first(With or without leading zero), followed by a dash or a space if I can't(or don't want to) use a dash, then month in either either form then another dash or space if I can't(or don't want to) use dash, then a year being last two digits or full year digits. I don't really like any other format, like `MM-DD-YY` or backwards `YY-MM-DD`, it just looks weird to me and am more accustomed to my preferred format if at all possible, which it is.... – Newb Coder Sep 11 '18 at 00:14
  • ...and I've ran out of character space so gonna have to make a new post; just like how some people prefer it a specific way to do things, I prefer it this way for my date format, and since the date format is this way anyways in Australia so I may as well follow and stick with convention.......so that's the story.... – Newb Coder Sep 11 '18 at 00:16

1 Answers1

0

still not quite sure, I got you right. Please try the following code and examine it's output, if it does, what you want.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set m=0
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
  set /a m+=1
  set "m[!m!]=%%a"
)

for /l %%Y in (2010,1,2020) do (
  for /l %%M in (1,1,12) do (
    set /a mm=%%M+100
    set "mm=!mm:~-2!"
    if exist "*!mm!-%%Y*.log" (
      ECHO md "!mm! !m[%%M]! %%Y" 2>nul
      ECHO move "*!mm!-%%Y*.log" "!mm! !m[%%M]! %%Y\"
    )
  )
)

I disarmed the md and move commands for security reasons

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • What security reasons is there to disable the md and move commands?! hahahaha.... Anyways, apologies for late reply, my computer decided to have a fit, mmm, it's close - with the numbering, initially wanted it to start at 01, even if Jan didn't exist, so for example I only had a log in Dec 2017 but no other logs in other months, it would make a folder called 01 December 2017 and move the files in there; but I suppose this'll do. Was also looking at the for loop and how it works by myself but I guess you already beat me to that before I could reply back with any results. – Newb Coder Sep 24 '18 at 05:39
  • Sure wish I could make mini paragraphs in comments for them to be digestible, anywho; so far from your code, it misses being moved up one level and creating folders there and moving the files to there, and I think that's it, the speed is almost instantaneous and pure cmd, rather than a mix of cmd + powershell - yeah this will do, perfect thanks! Just going to increase the end year date from 2020 to something higher.... – Newb Coder Sep 24 '18 at 06:06
  • Did I edit this right? `@echo off SETLOCAL ENABLEDELAYEDEXPANSION set m=0 for %%a in (January February March April May June July August September October November December) do ( set /a m+=1 set "m[!m!]=%%a" ) for /l %%Y in (2010,1,2050) do ( for /l %%M in (1,1,12) do ( set /a mm=%%M+100 set "mm=!mm:~-2!" if exist "*!mm!-%%Y*.log" ( md "..\!mm! !m[%%M]! %%Y" 2>nul move "..\*!mm!-%%Y*.log" "!mm! !m[%%M]! %%Y\" ) ) )` – Newb Coder Sep 24 '18 at 06:09
  • Actually you know what? Screw the comment formatting, here's a pastebin link: https://pastebin.com/yMHzbE6X Much prettier! ☺ Damn 5 minutes does fly pretty quick.... So when I run that modified code, I get "`A duplicate file name exists, or the file cannot be found.`" – Newb Coder Sep 24 '18 at 06:15
  • "security reason" is to prevent your data from being scratched/renamed before you had the choice to check if it does what you want. Imagine, the `ren` command does not exactly what you want - you may be left with a mess of falsely renamed files (maybe even not consistent, so you would have a lot of work undoing it) – Stephan Sep 24 '18 at 06:38
  • You process files in the *current* folder, `if` checks the existence in the *current* folder and then you try to move from the *parent* folder `..\`. Therefore `the file cannot be found`. – Stephan Sep 24 '18 at 06:41
  • AH, well don't worry about that - I'm only using test log files (essentially copies of their genuine counterpart, but for this batch testing before I do it on the whole thing as intended...)so there would be no harm even if these files got deleted or move somewhere that they are not meant to be. ☺ – Newb Coder Sep 24 '18 at 09:46
  • yes, working on copies (or having a backup) while developping is the way to go. But believe it or not - you're an extreme exception here... – Stephan Sep 24 '18 at 09:55
  • Ah, I see this is the correct line: `move "*!mm!-%%Y*.log" "..\!mm! !m[%%M]! %%Y\"` not `move "..\*!mm!-%%Y*.log" "!mm! !m[%%M]! %%Y\"` – Newb Coder Sep 24 '18 at 12:06