Computer Hope

Microsoft => Microsoft DOS => Topic started by: AT005 on November 26, 2009, 02:19:38 PM

Title: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 02:19:38 PM
Hi everyone

Finally I found some forum with a section for CMD topic and I hope I could find my so long searching answers here. If someone could take a look at my two issues about CMD, I would appreciate it a lot:

I am using the following command:

dir /ad /s /b > filename.doc

but to get what I need, I can change to command to something else (instead of just adding new parameters).

1. Could I get as the result (text inside file filename.doc) only two parts of each line? What I mean is this:

I have the line that looks like:

C:\Main Folder\Subfolder\Subsubfolder\[blabla] example_of_folder_name

I would like to have this only:

Subsubfolder\[blabla]

In each of line (no matter how many hundreds or thousands of lines) only those two parts.

2. Here is one screenshot that I made:

http://img.photobucket.com/albums/v309/Andreii/folder1.jpg

Lets say here are two groups of folders: first group is in the red square, second is in the green square. This is just an example. In the second group I usually have 1000+ folders (and not just 5) but the amount of folders is irrelevant comparing to my second issue.  What I would like to have are two different .doc files - one for first group of folders and one for second group. All of the subfolders, subsubfolders etc (just filenames not as my cmd command above shows) must always be in the .doc lists but in the format of text (some pieces of lines must be cutted) about which I was asking in my first issue.

Hopefully someone could help.

Thank you

Kindest regards
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Helpmeh on November 26, 2009, 02:23:47 PM
Will it always be the same level of folders deep?
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 02:46:48 PM
Will it always be the same level of folders deep?

Thank you for reply. Im not exsactly sure what you mean with ''folders deep'' but I can presume that you meant how many folders are before (''above'') subsubfolder. In this example:

C:\Main Folder\Subfolder\Subsubfolder\[blabla] example_of_folder_name

There are two folders before.

If I presumed wrong, please tell me what you meant with ''folders deep''.

If I presumed correctly then:

In the second group (green square): yes. Folders will always look like:

C:\Main Folder\Subfolder\Subsubfolder\[blabla] example_of_folder_name

So im always interested at ''Subsubfolder\[blabla]'' only.

In the first group (red square): no. Between ''Subsubfolder'' and ''[blabla]'' are another unlimited amount of folders located. So the path look like this:

C:\Main Folder\Subfolder\Subsubfolder\Subsubsubfolder\[blabla] example_of_folder_name

So im always interested at ''Subsubsubfolder\[blabla]'' only.

I also need to mentoin the following because it might change the answers on the questions that im interested for:

In the first group (red square) are exsactly 7 subsubfolders - Im talking only about subsubfolders and NOT any other ''depth'' of folders. Those subsubfolders will always be there, always with the same number of them (7) and always with the same names.
The part of the latest (the most deepest) folder that Im interested for will always be between ''['' and '']'' (i would prefer to have those two symbols written in the .doc file too) BUT i cannot guarantee there will always be a space between '']'' and first next letter (of if is number of even symbol).
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Helpmeh on November 26, 2009, 02:54:23 PM
Here you go! (And you did assume right :))

for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 03:05:06 PM
I get error message ''%%a was unexpected at this time"
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Helpmeh on November 26, 2009, 03:11:54 PM
Are you running it from the command prompt or a batch file?
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 03:12:50 PM
command prompt

start menu / run / ''cmd''
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: BatchFileBasics on November 26, 2009, 04:16:24 PM
thats the problem, you can't use for loops in the command prompt without a script.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 04:21:37 PM
thats the problem, you can't use for loops in the command prompt without a script.

Thank you for reply. And how do I make this so the command which Helpmeh typed will work? I remember somewhen I have used to read that it needs to be written in notepad and saved as .bat extension but not sure if this is correct. In the folders I have very important files for myself and I will not just ''test'' something because I THINK its correct so I wouldn't damage something or do something wrong. I prefered to ask for proven, correct solution instead of trying the luck.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Helpmeh on November 26, 2009, 04:29:08 PM
Yes.
Just put

@echo off

as the first line, then add my code on the next line. Add

pause

on the line after my code, and save it as file.bat .
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 04:46:01 PM
I got the .bat file now, Im pretty sure its correct. Also the icon got changed from notepad's one to ms-dos' one. Sorry about the question but how do I use this? I don't have much experience with scripting...
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Helpmeh on November 26, 2009, 05:13:45 PM
I got the .bat file now, Im pretty sure its correct. Also the icon got changed from notepad's one to ms-dos' one. Sorry about the question but how do I use this? I don't have much experience with scripting...
Just double-click on it. Now that you know what that icon looks like, I want to impress on you never to run/open files you haven't seen before and/or don't know what it does. For all .bat files, you can right-click and select Edit to see the source code. If you don't know what the code does, post it here and we can tell you what it will do.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 05:17:33 PM
I have done this (been in correct folder) but nothing happens.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: gh0std0g74 on November 26, 2009, 05:50:45 PM
Here you go! (And you did assume right :))

for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b
what if there are more levels deep? can't really have tokens 4,5 right?
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 26, 2009, 11:34:56 PM
what if there are more levels deep? can't really have tokens 4,5 right?

First group of folders (red square on the screenshot), for one out of two doc files, will always have its own levels depth. Also the second group of folders (green square on the screenshot), for the second doc file, will always have its own levels depth too - both kind of depths will be the same as stated above.

Hopefully I can make this .bat file to work. Helpmeh Is it an error in the source code? I typed inside exsactly this:

@echo off
for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b
pause

According to the message (some kind of ''press any key to continue'') that im getting, I cannot call this message an Error but still, because of it, im not getting the result (new two files in .doc extension). I think the ''pause'' might be the reason for that...
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Salmon Trout on November 27, 2009, 12:27:42 AM
thats the problem, you can't use for loops in the command prompt without a script.

That is not so; at the prompt you just use one % sign in the loop variables (instead of two)

Code: [Select]

S:\Test>for /f "tokens=1,2,3,4,5 delims=\" %a in ('dir /ad /s /b') do @echo %a\%b\%c\%d\%e

S:\Test\Batch\Older\custom-path
S:\Test\Other\Auto-it\
S:\Test\Other\Basic\
S:\Test\Other\beep\
S:\Test\Other\cURL\
S:\Test\Other\FTP\
S:\Test\Other\FUS-Repair\
S:\Test\Other\getmail\
S:\Test\Other\ghacker\
S:\Test\Other\hta\
S:\Test\Other\kix\
S:\Test\Other\Mirakagi\
S:\Test\Other\NATW\
S:\Test\Other\PopClient\
S:\Test\Other\Portable-Ubuntu\
S:\Test\Other\Python\
S:\Test\Other\reg\
S:\Test\Other\Ruby\
S:\Test\Other\textview\
S:\Test\Other\UnxUtils\
S:\Test\Other\virtual disk\
S:\Test\Other\wget_GUI\
S:\Test\Other\winmsg\
S:\Test\Other\winWget\
S:\Test\Other\wmv-avi\
S:\Test\Other\Zerodir\
S:\Test\Other\Auto-it\Auto-it3
S:\Test\Other\Auto-it\Checkbox
S:\Test\Other\Auto-it\MsgBox
S:\Test\Other\Auto-it\Auto-it3
S:\Test\Other\Auto-it\Auto-it3
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 28, 2009, 09:26:14 AM
hello?
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 29, 2009, 04:35:43 AM
I tried with:

Code: [Select]
@echo off
for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b

instead of

Code: [Select]
@echo off
for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b
pause

But even this doesn't do anything.  :(
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Salmon Trout on November 29, 2009, 05:50:07 AM
You are doing something wrong. We do not know what, since you don't give enough information.

Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 29, 2009, 08:44:38 AM
You are doing something wrong. We do not know what, since you don't give enough information.

I have tried to use the both codes - of course NOT at the same time. I have pasted them to notepad, each time i saved them as .bat. Firstly used second code but since it doesn't work, I used first one but doesn't work too - two .doc files are still not created. What I wish to have in .doc files is described in my first and second post.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Salmon Trout on November 29, 2009, 08:47:31 AM
You left the > filename.doc off the end.
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on November 29, 2009, 08:52:06 AM
If you meant this:

Code: [Select]
@echo off
for /f "tokens=4,5 delims=\" %%a in ('dir /ad /s /b') do echo %%a\%%b > filename.doc
pause

It does give me the .doc file but just one instead of two and with terrible results inside - completely different from what I was asking  ???
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: Salmon Trout on November 29, 2009, 08:54:41 AM
I think you are going to have to learn some batch scripting
Title: Re: Command Prompt (CMD); Two issues about DIR command
Post by: AT005 on December 04, 2009, 01:30:47 PM
Why nobody wants to tell me what should I do, in my situation, described in first and third post?  :(