0

I need to make a script that can be executed from an external device, (like a pendrive).

I used this command line:

dir %SYSTEMDRIVE% /B /S >> .\report\%COMPUTERNAME%-dirlist.txt

When I execute from cmd.exe, it works fine, but when I execute the script, it only lists the directories that are in the same folder as the script.

Can anyone tell me why that's happening?

Heres the full file, (relatório is just report on my language)

@echo off
IF exist .\relatorios ( break ) ELSE ( mkdir .\relatorios\)

::1) Salvar lista de usuários do equipamento e seus papéis.

echo =========================== >> .\relatorios\%COMPUTERNAME%-userslist.txt
echo * %DATE% %TIME% >> .\relatorios\%COMPUTERNAME%-userslist.txt
echo =========================== >> .\relatorios\%COMPUTERNAME%-userslist.txt
dir %SYSTEMDRIVE%\users /B /ON >> .\relatorios\%COMPUTERNAME%-userslist.txt

::2) Salvar Lista de Diretórios contidos na raiz.

echo =========================== >> .\relatorios\%COMPUTERNAME%-dirlist.txt
echo * %DATE% %TIME% >> .\relatorios\%COMPUTERNAME%-dirlist.txt
echo =========================== >> .\relatorios\%COMPUTERNAME%-dirlist.txt
dir %SYSTEMDRIVE% /B /S >> .\relatorios\%COMPUTERNAME%-dirlist.txt

::3) Salvar a lista de processos em execução no momento

echo =========================== >> .\relatorios\%COMPUTERNAME%-pslist.txt
echo * %DATE% %TIME% >> .\relatorios\%COMPUTERNAME%-pslist.txt
echo =========================== >> .\relatorios\%COMPUTERNAME%-pslist.txt
tasklist >> .\relatorios\%COMPUTERNAME%-pslist.txt

::4) Gerar um relatório CSV

hostname >> .\computadores.csv

::5) Desligar o equipamento

echo shutdown /p

It's supposed to be a menu with 5 options, but I'd like to get them all to work before making the menu part.

Can anyone help me with this?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • I can't replicate your problem. `dir %SYSTEMDRIVE% /B /S /A:D>> .\report\%COMPUTERNAME%-dirlist.txt` correctly lists all folders on my C: drive (once I removed the space, added the `/a:d` for specifying only folders, and created the `report` folder). I'm also running it from a different drive. – SomethingDark Apr 03 '23 at 14:30
  • thats exacly why i came here for help, im not sure why that happened. i can provide the full file if that helps – João Vitor Chivite Apr 03 '23 at 14:47
  • If you're running other commands before the `dir` command, your question should [be edited](https://stackoverflow.com/posts/75920392/edit) to include those in case they're relevant. – SomethingDark Apr 03 '23 at 14:50
  • I edited to add the code , i don't think the rest of the code would afect this one but it could be the case. thank you for the sugestion – João Vitor Chivite Apr 03 '23 at 14:57
  • 2
    `dir %SYSTEMDRIVE% /B /S /A:D` does __not__ output all directories on the system drive. It outputs just all directories in the __current directory__ of the system drive because of `%SYSTEMDRIVE%` expands usually to `C:` which is a relative path as described by Microsoft in the documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file). The correct command is `dir %SYSTEMDRIVE%\ /AD-L /B /S` which runs the recursive search from root directory of system drive and excludes junctions and symbolic links to folders. – Mofi Apr 03 '23 at 16:18
  • __Note 1:__ A space on an `echo` command line left to the redirection operator `>>` is output by the command `echo` and for that reason also written as trailing space into the text file. I suggest reading [Why does ECHO command print some extra trailing space into the file?](https://stackoverflow.com/a/46972524/3074564) There is explained how to avoid the trailing space on lines output with `echo`. It should be fail-safe in this case to just remove the space left to `>>` on each line starting with `echo`. – Mofi Apr 03 '23 at 16:21
  • __Note 2:__ The first condition `IF exist .\relatorios ( break ) ELSE ( mkdir .\relatorios\)` should be replaced by `if not exist "%~dp0relatorios\" mkdir "%~dp0relatorios"` to create the directory `relatorios` __not__ in __current directory__ on not existing, but in the __batch file directory__ on not existing there. __Note 3:__ There should be used next `set "ResultsFile=%~dp0relatorios\%COMPUTERNAME%-"` and used on the redirections `"%ResultsFile%userslist.txt"`, `"%ResultsFile%dirlist.txt"`, and `"%ResultsFile%pslist.txt"`. – Mofi Apr 03 '23 at 16:27
  • __Note 4:__ `::` is not the beginning of a comment, it is an invalid label. There should be used the remark command `rem` to begin a comment and not the invalid label `::`. __PS:__ I rate a batch file executed from a USB pen drive which collects data from host system on the pen drive and then shuts down the host as malicious batch file. It´s good that this batch file cannot be run ever automatically on just plugging in the USB pen drive on a Windows computer. – Mofi Apr 03 '23 at 16:30
  • its not malicious its what my teachers asked us to do as homework i was using what he said in class but everyting is kind of new to me, and i thank you a lot for all this useful comments. i supposethe teacher must be teaching us basic commands before teaching how to proper organize the code – João Vitor Chivite Apr 03 '23 at 16:56
  • for the menu part, I suggest `choice /?` – Stephan Apr 03 '23 at 17:13
  • thank you Stephan, I was a bit worried about that part, our teacher usaed a choice menu using an .EXE file instead but I suppose choice /? is way better – João Vitor Chivite Apr 03 '23 at 17:22
  • well - `choice` **is** an `.EXE` file... `:D` – Stephan Apr 03 '23 at 17:29
  • Please read [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) It explains in full details how to use the [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) __CHOICE__ which is like most *EXTERNAL* Windows commands an executable in `%SystemRoot%\System32` in comparison to the *INTERNAL* Windows commands of `cmd.exe`. – Mofi Apr 04 '23 at 07:46
  • Hint: A Windows command executed with `/?` and printing the usage help but resulting in an error message on running `where` with the command as argument is an internal command of `cmd.exe`. All others are external commands and `where` outputs the fully qualified file name. Try it out with `if /?` and `where if` and next with `where /?` and `where where` or `choice /?` and `where choice`. – Mofi Apr 04 '23 at 07:49
  • Last hint: There is on SS64 the [A-Z index of Windows CMD commands](https://ss64.com/nt/) where most Windows commands are explained better than in the Microsoft documentation. I recommend to read the [mklink](https://ss64.com/nt/mklink.html) documentation explaining the different types of links which are referenced in help of __DIR__ for letter `L` (link) on explanation of option `/A` (attributes) with the term *reparse points*. – Mofi Apr 04 '23 at 07:55

1 Answers1

0

I'd like to thank Mofi for providing not only the answer, but also very useful comments that will help me improve in class:

dir %SYSTEMDRIVE% /B /S /A:D does not output all directories on the system drive. It outputs just all directories in the current directory of the system drive because %SYSTEMDRIVE% expands usually to C: which is a relative path as described by Microsoft in the documentation about Naming Files, Paths, and Namespaces. The correct command is dir %SYSTEMDRIVE%\ /AD-L /B /S which runs the recursive search from the root directory of the system drive and excludes junctions and symbolic links to folders." – Mofi

I'd like to also thank Compo for helping formatting my post.

I apologize for any mistakes or typos. I'm new to this site. I'm not sure how to properly credit Mofi for the answer. I hope this is the correct way, but if it isn't I'll make sure to do it properly.

Stephan
  • 53,940
  • 10
  • 58
  • 91