|
Answer» Hey all.
I've been scratching my head over this for some time now;
Code: [Select]@echo off set /p name="Skriv elev navn(e): " echo 1 md %name% echo 2 echo %name%>>folderlog.txt echo 3 echo %name%>%name%\%name%.txt echo 4 pause This is the code I've got so far, obviously the echo's are for debugging.
This is the aim of this script;
To create as many folders as required, in this case student names - e.g.: John, Jake, Peter etc. I need it to create the folder, a .txt file within the newly created folder containing the number the folder was during creation - and the name of the folder. As such: MAIN DIR\John\John.txt - containing string: , . I hope this is clear enough. I have yet to figure out the numbering issue, and it's a real head-scratcher. However, I have successfully managed to get it to create the .txt file with "John" in it. However, when I try to make more than one folder - the script breaks only creating the folders, and no .txt files. This happens at the .txt file creation string. That's basically where I'm stuck. I've worked on it for hours today, with no luck.
Also, I've been attempting to make a batch file that will delete all *folders* including their contents in a directory. So far my attempts have been unsuccessful.
I really hope this great site - and it's members can provide me with help for a solution to this.
Thanks in advance!
-Frobergtry this for numbering
@echo off :labl1 set /p name="Skriv elev navn(e): " echo 1 for /f %%i in ('dir /a ^|find /c "<DIR>"') do set /a d=%%i md %name% echo 2 echo %name%>>folderlog.txt echo 3 echo %d%,%name%>"%name%\%name%.txt" echo 4 set re= set /p re=to insert an other student press r if "%re%" EQU "r" goto labl1 pause
i hope its helpfulIt is indeed. Only, it creates the names one at a time.. I'd really prefer to just type in a long string of names... but that's not possible I take it? Been playing around with it a bit, any chance of getting it to start counting with number 1 instead of number 2?
I can't see that particular oddity being set in the script..at first i forgot to till u that the u have to remove the /a from the dir command in the for LOOP thats due to it will give u 2 aditional directoryes that are . and .. for that u have to remove the /a unless u got som headin folders u want to cont
and about the names u can put them into a file line by line an do this
setlocal ENABLEDELAYEDEXPANSION for /f "delims=" %%i in (names.txt) do ( set name=%%i echo 1 for /f %%t in ('dir ^|find /c ""') do set /a d=%%t md "!name!" echo 2 echo !name!>>folderlog.txt echo 3 echo !d!,!name!>"!name!\!name!.txt" echo 4 ) pause endlocal Nah, it has to be direct user input.
I was wondering if you could look at SOMETHING else, perhabs.
I'm trying to make a batch to delete all the directories, and the contents - but leave the files in root intact.
Here's what I've got so far; Code: [Select]@echo off dir /b /ad %1 > dirlist.txt for /f %%i in (dirlist.txt) do rd /s /q %1\%%i Basically creating a directory list, and have it delete the folders listed. However, it says "File not found" in the cmd prompt. Any ideas?
I really appreciate your help man.Quote To create as many folders as required, in this case student names - e.g.: John, Jake, Peter etc. I need it to create the folder, a .txt file within the newly created folder containing the number the folder was during creation - and the name of the folder. As such: MAIN DIR\John\John.txt - containing string: <number>, <John>.
Code: [Select]@echo off setlocal enabledelayedexpansion set count=0 set /p name=Skriv elev navn(e): for %%v in (%name%) do ( call set /a count=%%count%%+1 md %%v echo ^<!count!^> ^<%%v^> >> %%v\%%v.txt )
Enter all the names, each separated by a space, at the prompt. (ie. John Jake Peter)
Quote@echo off dir /b /ad %1 > dirlist.txt for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
Code: [Select]@echo off for /f "tokens=* delims=" %%i in ('dir /b /a:d %1') do ( rd "%1\%%i" /s /q )
You might want to add a prompt for the directory name, rather than passing it along the command line.
Good luck. Excellent. I've got it working now.
So, the other query. Any way I can get a batch file to delete all folders in, for EXAMPLE, C:\data\project\blah - and leave the other files in that directory intact?
All the methods I've tried, have either deleted the whole folder, or just deleted my files and left the directories alone.
I would only need to keep two batch files in the root, everything else should go.
The "Make folder" batch and the "delete all dirs" batch file.
Thanks a bunch guys!QuoteSo, the other query. Any way I can get a batch file to delete all folders in, for example, C:\data\project\blah - and leave the other files in that directory intact?
This was asked and answered in the previous post:
Code: [Select]@echo off for /f "tokens=* delims=" %%i in ('dir /b /a:d %1') do ( rd "%1\%%i" /s /q )
As mentioned previously, you may want to prompt for the directory name instead of passing it along the command line.
Prompt for directory name?
It should delete all folders in that directory, including their contents - without removing files in the root directory.. The two batch files, and whatever .txt files may be present. Quote@echo off dir /b /ad %1 > dirlist.txt for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
In your original code you referenced the %1 variable which in most cases is defined as an argument on the command line.
QuotePrompt for directory name?
Yes, just like you did in the other batch file: Quoteset /p name="Skriv elev navn(e): "
If batch file is to run against the current directory, remove the reference to %1 Code: [Select]@echo off for /f "tokens=* delims=" %%i in ('dir /b /a:d') do ( rd "%1\%%i" /s /q )
Oh my GOD! I love you! YOU ARE MY NEW BEST FRIEND!
That piece of code works FLAWLESSLY..
I mean, WOW!
|