Find all users, use them as variable to pull appdata directory list from cmd? - scripting

I'm trying to build out a script that will pull the list of users, grab each of their names and then do a directory list for each of their appdata folders.
So far I'm thinking that I could just use the net user > C:\userlist.txt to create the list of users, my real problem comes in grabbing the user's names and putting them into the next command.
By using a variable of some kind I would like to have findstr pull in each user so that I could get a list of all the users's directories.
The end result would look something like this:
dir C:\users\$variableforusers\AppData\Roaming > C:\directorylist.txt
I'd like to be able to get this command to run for each users appdata directory, so this command would need to repeat itself based on how many users the end machine has.
This will be running from the local system account so %appdata% will not work as it only gives you the currently logged in user's appdata folder.
If possible, I'd like for the directory list to all be dumped into one file, otherwise I'd have to introduce a variable to check multiple files for the results.
Thanks ahead of time for the help guys!

I think you should not use user's name to obtain a path to user's profile directory. The directory name can be different than user's name - for example in case user name hsa been changed after the profile was created or if there is a user name conflict (a local user has the same username as a domain user).
You can do a dir of AppData\Roaming for each subdirectory of C:\Users using the following syntax:
for /d %A in (C:\Users\*) do if exist %A\AppData\Roaming dir %A\AppData\Roaming
But again this solution will omit any user profiles that are stored outside C:\Users.
To do a dir of AppData\Roaming directory of each user profile, you can get the paths of user profiles from registry:
for /f "tokens=3" %A in ('reg query "HKLM\Software\Microsoft\WindowsN T\CurrentVersion\ProfileList" /s /v ProfileImagePath ^| find "REG_EXPAND_SZ"') do #dir %A\AppData\Roaming
If you want to use these commands in a script, use double percent signs (%%A).
To learn more about for command type for /?

Related

From where the Stored Procedure file will be called when using #filename in SQL plus

I have the following SQL statement: SELECT sys_context('userenv','db_name') FROM dual;. I stored it inside a SQL file called db.sql in C:
Now, I can run this file:
#C:\db.sql
I know we can invoke the file just as #db, but, when I try it, I get this error:
SQL> #db
SP2-0310: unable to open file "db.sql"
So, where should I place the file? I read here http://docs.oracle.com/html/B12033_01/sqlplus.htm that I have to edit some variable names like SQLPATH. Where should I edit this?
SQLPATH seems like an environment variable.
RClick on your MyComputer. Go to "Advanced System Settings". Under "Advanced" Tabs click on "Environment Variables..." and there you can set the SQLPATH under your system variables if you have admin rights.
Found the answer myself :) here sharing this...
Always these files will be looked in Bin of your Oracle Client. Most user create ORACLE_HOME environmental variable at time of installation and the files will be looked in ORACLE_HOME\Bin
So create a environmental variable if not present already pointing
to [driver/folder_installed]/product/{version}/client
Place the files in bin
that's all enjoy!
Under Windows, when you run sqlplus it will first look in the same location where sqlplus was run from - so I just edit my sqlplus shortcut "Start In" to be the folder where my scripts are located.

User Input Operation in Windows batch Programming

I want to take input from the user in ddmmyyyy format and when user enters the date in this format then files would move to the respective folder. I tried in the following code but failled.
SET /p str=Enter the name of the folder e.g. 30062011:
move C:\Documents and Settings\MyPC\Desktop*_%str%.pdf S:**%str%**
What is failing? Move or accepting user input?
I tried following, and I am able to read the user input
#echo off
set /P theuserinput="Enter the filename: "
echo %theuserinput%

Powershell - Trying to test-path against a string that resembles a variable name

The company I work for prefixes all their admin level accounts with a $ sign. Trouble, I know.
I'm trying to use test-path to see if a folder exists like so:
$username = read-host "Enter Login ID:"
I type $adminsdb into the box and hit ok
##### Find TS Profile #####
$TSProfile_exist = test-path "\\server\tsprofiles$\$username"
The folder DOES exist but... $TSProfile_exist is coming up False
How do I handle the $ in the username? I'm building this app to bring up quick stats on users in the environment. We also have service accounts that are prefixed with # signs.
The way to handle special caracters in PowerShell is using ` (backtick)
$TSProfile_exist = test-path "\\server\tsprofiles$\$username"
becomes
$TSProfile_exist = test-path "\\server\tsprofiles`$\$username"
Be careful a trick with Test-Path, is that it semantic is not that the directory exists, but is that the directory is readable. In other words if you do not have access to the directory you test, you will receive false even if the directory exists. See this other entry.
The other alternative is to use single-quoted strings; powershell will not expand variables in this case.
test-path '\\server\path\$username'
-Oisin

Using xcopy to copy files from several directories to one directory

Is it possible to use xcopy to copy files from several directories into one directory using only one xcopy command?
Assuming that I have the directory tree
root\Source\Sub1\Sub2
I want to copy all .xml files from the directory root\Source including sub folder to root\Destination. I don't want to copy the folder structure, just the files.
As DandDI said, you don't need xcopy. for statement helps much. However, you don't need to state process outcome of dir command as well, this command helps better
for /R c:\source %f in (*.xml) do copy "%f" x:\destination\
By the way, when you use it from a batch file, you need to add spare % in front of variable %f hence your command line should be;
for /R c:\source %%f in (*.xml) do copy %%f x:\destination\
when you use it within a batch
Should surround %f with double quotes otherwise it will fail copying file names with spaces
You don't need xcopy for that.
You can get a listing of all the files you want and perform the copy that way.
For example in windows xp command prompt:
for /f "delims==" %k in ('dir c:\source\*.xml /s /b') do copy "%k" x:\destination\
The /s goes into all subdirectories and the /b lists only the files name and path. Each file inturn is assigned to the %k variable, then the copy command copies the file to the destination. The only trick is making sure the destination is not part of the source.
The Answer to this problem which I think is "How to gather all your files out of all the little subdirectories into one single directory" is to download a piece of software called XXCOPY. This is freely available via XXCOPY.COM and there's a free non-commercial version fortunately. One of the Frequently Asked Questions on the help facility on XXCOPY.COM is effectively "How do I gather all my files into one directory" and it tells you which switch to use. XXCOPY is though a surefire way of doing this and it comes in a .zip archive so unzipping it can be not that straightforward but it's not particularly difficult either. There is an unzipping program called ZipGenius available through the ZipGenius.it website so maybe before you download XXCOPY then download ZipGenius then it's a smallpart smalltime double wammy(!)
Might not be the exact answer but if anyone would like to do this without coding.
You can search the name of the item inside a specific folder, and then you can copy the results and later paste it into your desired folder. It will rename the same file to be the folder I believe as the prefix and then the repeated name.

How to find any "txt" file at particular location in system?

I have a robot to find a file of the given name at a particular location in a system but now I want to find all the text files at that particular location. I have tried to use "*.txt", but it didn't worked out. Is there a way to do that?
file.exists ♥environment⟦USERPROFILE⟧\Documents\t.txt errormessage ‴Sorry, I could not find a file‴
dialog ‴File exists‴
You can use the directory command. The pattern arguments allows you to filter out files of a particular extension.
directory path ♥environment⟦USERPROFILE⟧\Desktop pattern *.txt result ♥files
dialog ♥files⟦count⟧
The above code should let you know how many files of the given extension exist in the given directory.
You could take values from the returned list and use it with file.exists command.