create folders from a list and move files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#46 Post by Endoro » 24 May 2013 00:08

sifar786 wrote:hi Endoro,

Though i still fail to understand what

Code: Select all

SET "."
means or does, i have got the code working by changing a few things. for e.g. the %targetfolder% was not specified while making the directories. i also eliminated the linefeeds between each statements and also removed wrap text so that each command could fall on its own line.

However, i am not getting the same output as shown before now:

Sorry, but I can't do anything to get unknown code working.

Code: Select all

Z:\Output Folder>set/p"=JPG   "0<nul
This is ordinary input redirection.

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#47 Post by sifar786 » 24 May 2013 06:17

hi Endoro,

What i am saying is, right now the output differs from what it was previously. There is no ID column shown and so all the ID's are missing from the output file, so i dont know which ID's were processed.

see previous:

Code: Select all

cls     doc     docx    jpg     mdb     pdf     png     ppt     xls     xlsm
10001   0       0       0       3       1       2       0       0       0       0
10002   1       0       1       1       0       0       1       0       0       0
10003   0       1       0       0       0       0       0       1       0       1
10004   0       0       0       0       0       0       0       0       1       0


see now:
see previous:

Code: Select all

cls     doc     docx    jpg     mdb     pdf     png     ppt     xls     xlsm
0       0       0       3       1       2       0       0       0       0
1       0       1       1       0       0       1       0       0       0
0       1       0       0       0       0       0       1       0       1
0       0       0       0       0       0       0       0       1       0


i am still using your latest code but this doesn't output the ID:

Code: Select all

((for /f "delims==." %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
(for /f "tokens=2 delims==" %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
)>"%CSVfile%"


Also, here i had to add %targetfolder% in 2nd line i.e. if not exist.... :

Code: Select all

for /f "usebackqtokens=1,2" %%i in ("%ClassID_ID_List%") do (
   if not exist "%%i\%%j\" md "%targetfolder%\%%i\%%j"
   move "%%j_*" "%targetfolder%\%%i\%%j"
)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#48 Post by Endoro » 24 May 2013 08:43

I stopped counting ID's after your posting here:
sifar786 wrote:I think my original query was to get every extension found in a folder and display and not limit it to a fixed few.

So if you suddenly need ID counting again, please use my previous code.



Yes, this has to be modifyed:

Code: Select all

for /f "usebackqtokens=1,2" %%i in ("%ClassID_ID_List%") do (
   if not exist "%targetfolder%\%%i\%%j\" md "%targetfolder%\%%i\%%j"
   move "%%j_*" "%targetfolder%\%%i\%%j"
)

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#49 Post by sifar786 » 24 May 2013 11:34

hi Endoro,

I think there was a miscommunication at that time as i was not able to get the results on screen, so really didnt know how the result looked.

The earlier code was supposed to work with Local drives and not network drives, hence finding it difficult to write the line in your current code which adds the ID column.

i think i understand that this code is creating variables in memory OR Screen,

Code: Select all

for /f "usebackq tokens=1" %%i in ("%ClassID_ID_List%") do (for %%f in ("%%i_*") do set /a %%~xf=%%~xf + 1 2>nul)


outputting,

Code: Select all

.JPG=7   .mov=1   .ppt=1   


which is then parsed accordingly by the following generated prompts,

Code: Select all

((for /f "delims==." %%i in ('set "."') do <nul set /p"=%%i%CSVdelim%")&echo(
(for /f "tokens=2 delims==" %%i in ('set "."') do <nul set /p"=%%i%CSVdelim%")&echo(
 )>"%CSVfile%"


to give the following:

Code: Select all

JPG   mov   ppt   
7   1   1   


i think

Code: Select all

set "."
gives access to those variables on screen or memory.
so i think the code

Code: Select all

for /f "usebackq tokens=1" %%i in ("%ClassID_ID_List%") do (for %%f in ("%%i_*") do set /a %%~xf=%%~xf + 1 2>nul)
needs to be modified as it does not display the ID's for the remaining code to parse. But How?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#50 Post by Endoro » 27 May 2013 11:25

Yes, you are right. But now one single dot is not enough, we need subgroups (ClassID):

Code: Select all

@echo off&setlocal
set "startfolder=%~1"      &rem parameter #1
set "targetfolder=%~2"      &rem parameter #2
set "ClassID_ID_List=%~3"   &rem parameter #3
set "CSVfile=%~4"         &rem parameter #4
set "CSVdelim=   " &rem, delimiter for output, default <tab>
rem            ^---< put your <tabspace> there

cd /d "%startfolder%"
if not exist "%ClassID_ID_List%" echo("%ClassID_ID_List%" not found&goto:eof
for /f "usebackqtokens=2" %%i in ("%ClassID_ID_List%") do call set spp= %%spp%% "%%i_*"
for /f "usebackqtokens=2" %%i in ("%ClassID_ID_List%") do (
 for %%f in (%spp%) do set /a .%%i%%~xf= 0 2>nul
)
for /f "usebackqtokens=2" %%i in ("%ClassID_ID_List%") do (
 for %%f in ("%%i_*") do set /a .%%i%%~xf+= 1 2>nul
)
set "." >nul 2>&1 || (echo(no files from "%ClassID_ID_List%" found in "%startfolder%"&goto:eof)
set/p "last="<"%ClassID_ID_List%"
(
<nul set/p"=ClassID%CSVdelim%"
for /f "tokens=2" %%i in ("%last%") do for /f "tokens=2delims==." %%f in ('set ".%%i"') do <nul set/p"=%%f%CSVdelim%"
echo(
for /f "usebackqtokens=2" %%i in ("%ClassID_ID_List%") do (
   <nul set/p"=%%i%CSVdelim%"
   for /f "tokens=3delims==." %%f in ('set ".%%i"') do <nul set/p"=%%f%CSVdelim%"
   echo(
))>"%CSVfile%"
for /f "usebackqtokens=1,2" %%i in ("%ClassID_ID_List%") do (
   if not exist "%targetfolder%\%%i\%%j\" md "%targetfolder%\%%i\%%j"
   move "%%j_*" "%targetfolder%\%%i\%%j"
)
type "%CSVfile%"


.. output is eg.

Code: Select all

C:\TEST\test\10001_ANU.jpg
C:\TEST\test\10001_COE.jpg
C:\TEST\test\10001_COE.pdf
C:\TEST\test\10001_ENU.jpg
C:\TEST\test\10001_ENU.mdb
C:\TEST\test\10001_ENU.pdf
C:\TEST\test\10004_GAR.xls
C:\TEST\test\10002_ENI.jpg
C:\TEST\test\10002_EVS.cls
C:\TEST\test\10002_TAS.docx
C:\TEST\test\10002_TNU.png
C:\TEST\test\10003_NIO.xlsm
C:\TEST\test\10003_NOE.doc
C:\TEST\test\10003_RAS.ppt
ClassID cls     doc     docx    jpg     mdb     pdf     png     ppt     xls     xlsm
10001   0       0       0       3       1       2       0       0       0       0
10004   0       0       0       0       0       0       0       0       1       0
10002   1       0       1       1       0       0       1       0       0       0
10003   0       1       0       0       0       0       0       1       0       1

Code: Select all

C:TEST2
├───1
│   ├───10001
│   │       10001_ANU.jpg
│   │       10001_COE.jpg
│   │       10001_COE.pdf
│   │       10001_ENU.jpg
│   │       10001_ENU.mdb
│   │       10001_ENU.pdf
│   │
│   └───10004
│           10004_GAR.xls

├───2
│   └───10002
│           10002_ENI.jpg
│           10002_EVS.cls
│           10002_TAS.docx
│           10002_TNU.png

└───3
    └───10003
            10003_NIO.xlsm
            10003_NOE.doc
            10003_RAS.ppt

that's a lot of code, and now I need a lot of coffee ...
& you can check, if & how this all works :D

Post Reply